Jacobi Iteration in numerical methods
In the following code I implemented Jacobi iteration. My code right now runs through $25$ iterations. If I wanted to change it to just run through one iteration how would I do that? Would I need to change my jacobi function?
from pprint import pprint
from numpy import array, zeros, diag, diagflat, dot
def jacobi(A,b,N=25,x=None):
"""Solves the equation Ax=b via the Jacobi iterative method."""
# Create an initial guess if needed
if x is None:
x = zeros(len(A[0]))
# Create a vector of the diagonal elements of A
# and subtract them from A
D = diag(A)
R = A - diagflat(D)
# Iterate for N times
for i in range(N):
x = (b - dot(R,x)) / D
return x
A = array([[2.0,1.0],[5.0,7.0]])
b = array([11.0,13.0])
guess = array([1.0,1.0])
sol = jacobi(A,b,N=25,x=guess)
print ("A:")
pprint(A)
print ("b:")
pprint(b)
print ("x:")
pprint(sol)
linear-algebra numerical-methods
add a comment |
In the following code I implemented Jacobi iteration. My code right now runs through $25$ iterations. If I wanted to change it to just run through one iteration how would I do that? Would I need to change my jacobi function?
from pprint import pprint
from numpy import array, zeros, diag, diagflat, dot
def jacobi(A,b,N=25,x=None):
"""Solves the equation Ax=b via the Jacobi iterative method."""
# Create an initial guess if needed
if x is None:
x = zeros(len(A[0]))
# Create a vector of the diagonal elements of A
# and subtract them from A
D = diag(A)
R = A - diagflat(D)
# Iterate for N times
for i in range(N):
x = (b - dot(R,x)) / D
return x
A = array([[2.0,1.0],[5.0,7.0]])
b = array([11.0,13.0])
guess = array([1.0,1.0])
sol = jacobi(A,b,N=25,x=guess)
print ("A:")
pprint(A)
print ("b:")
pprint(b)
print ("x:")
pprint(sol)
linear-algebra numerical-methods
I'm confused. You wrote the code and you do not know where does it control the number of iteration?
– Siong Thye Goh
Dec 4 '18 at 16:14
just didn't know if I had to change both iteration counts
– mt12345
Dec 4 '18 at 16:15
add a comment |
In the following code I implemented Jacobi iteration. My code right now runs through $25$ iterations. If I wanted to change it to just run through one iteration how would I do that? Would I need to change my jacobi function?
from pprint import pprint
from numpy import array, zeros, diag, diagflat, dot
def jacobi(A,b,N=25,x=None):
"""Solves the equation Ax=b via the Jacobi iterative method."""
# Create an initial guess if needed
if x is None:
x = zeros(len(A[0]))
# Create a vector of the diagonal elements of A
# and subtract them from A
D = diag(A)
R = A - diagflat(D)
# Iterate for N times
for i in range(N):
x = (b - dot(R,x)) / D
return x
A = array([[2.0,1.0],[5.0,7.0]])
b = array([11.0,13.0])
guess = array([1.0,1.0])
sol = jacobi(A,b,N=25,x=guess)
print ("A:")
pprint(A)
print ("b:")
pprint(b)
print ("x:")
pprint(sol)
linear-algebra numerical-methods
In the following code I implemented Jacobi iteration. My code right now runs through $25$ iterations. If I wanted to change it to just run through one iteration how would I do that? Would I need to change my jacobi function?
from pprint import pprint
from numpy import array, zeros, diag, diagflat, dot
def jacobi(A,b,N=25,x=None):
"""Solves the equation Ax=b via the Jacobi iterative method."""
# Create an initial guess if needed
if x is None:
x = zeros(len(A[0]))
# Create a vector of the diagonal elements of A
# and subtract them from A
D = diag(A)
R = A - diagflat(D)
# Iterate for N times
for i in range(N):
x = (b - dot(R,x)) / D
return x
A = array([[2.0,1.0],[5.0,7.0]])
b = array([11.0,13.0])
guess = array([1.0,1.0])
sol = jacobi(A,b,N=25,x=guess)
print ("A:")
pprint(A)
print ("b:")
pprint(b)
print ("x:")
pprint(sol)
linear-algebra numerical-methods
linear-algebra numerical-methods
asked Dec 4 '18 at 16:10
mt12345
958
958
I'm confused. You wrote the code and you do not know where does it control the number of iteration?
– Siong Thye Goh
Dec 4 '18 at 16:14
just didn't know if I had to change both iteration counts
– mt12345
Dec 4 '18 at 16:15
add a comment |
I'm confused. You wrote the code and you do not know where does it control the number of iteration?
– Siong Thye Goh
Dec 4 '18 at 16:14
just didn't know if I had to change both iteration counts
– mt12345
Dec 4 '18 at 16:15
I'm confused. You wrote the code and you do not know where does it control the number of iteration?
– Siong Thye Goh
Dec 4 '18 at 16:14
I'm confused. You wrote the code and you do not know where does it control the number of iteration?
– Siong Thye Goh
Dec 4 '18 at 16:14
just didn't know if I had to change both iteration counts
– mt12345
Dec 4 '18 at 16:15
just didn't know if I had to change both iteration counts
– mt12345
Dec 4 '18 at 16:15
add a comment |
1 Answer
1
active
oldest
votes
Nothing, just call the function jacobi
with a different argument
sol = jacobi(A, b, N = 1, x = guess)
This should give you
A:
array([[2., 1.],
[5., 7.]])
b:
array([11., 13.])
x:
array([5. , 1.14285714])
so I wouldn't need to change the original jacobi function?
– mt12345
Dec 4 '18 at 16:14
1
@mt12345 No, you don't need to change anything
– caverac
Dec 4 '18 at 16:15
thanks for your help!
– mt12345
Dec 4 '18 at 16:15
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3025754%2fjacobi-iteration-in-numerical-methods%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Nothing, just call the function jacobi
with a different argument
sol = jacobi(A, b, N = 1, x = guess)
This should give you
A:
array([[2., 1.],
[5., 7.]])
b:
array([11., 13.])
x:
array([5. , 1.14285714])
so I wouldn't need to change the original jacobi function?
– mt12345
Dec 4 '18 at 16:14
1
@mt12345 No, you don't need to change anything
– caverac
Dec 4 '18 at 16:15
thanks for your help!
– mt12345
Dec 4 '18 at 16:15
add a comment |
Nothing, just call the function jacobi
with a different argument
sol = jacobi(A, b, N = 1, x = guess)
This should give you
A:
array([[2., 1.],
[5., 7.]])
b:
array([11., 13.])
x:
array([5. , 1.14285714])
so I wouldn't need to change the original jacobi function?
– mt12345
Dec 4 '18 at 16:14
1
@mt12345 No, you don't need to change anything
– caverac
Dec 4 '18 at 16:15
thanks for your help!
– mt12345
Dec 4 '18 at 16:15
add a comment |
Nothing, just call the function jacobi
with a different argument
sol = jacobi(A, b, N = 1, x = guess)
This should give you
A:
array([[2., 1.],
[5., 7.]])
b:
array([11., 13.])
x:
array([5. , 1.14285714])
Nothing, just call the function jacobi
with a different argument
sol = jacobi(A, b, N = 1, x = guess)
This should give you
A:
array([[2., 1.],
[5., 7.]])
b:
array([11., 13.])
x:
array([5. , 1.14285714])
answered Dec 4 '18 at 16:13
caverac
13.9k21130
13.9k21130
so I wouldn't need to change the original jacobi function?
– mt12345
Dec 4 '18 at 16:14
1
@mt12345 No, you don't need to change anything
– caverac
Dec 4 '18 at 16:15
thanks for your help!
– mt12345
Dec 4 '18 at 16:15
add a comment |
so I wouldn't need to change the original jacobi function?
– mt12345
Dec 4 '18 at 16:14
1
@mt12345 No, you don't need to change anything
– caverac
Dec 4 '18 at 16:15
thanks for your help!
– mt12345
Dec 4 '18 at 16:15
so I wouldn't need to change the original jacobi function?
– mt12345
Dec 4 '18 at 16:14
so I wouldn't need to change the original jacobi function?
– mt12345
Dec 4 '18 at 16:14
1
1
@mt12345 No, you don't need to change anything
– caverac
Dec 4 '18 at 16:15
@mt12345 No, you don't need to change anything
– caverac
Dec 4 '18 at 16:15
thanks for your help!
– mt12345
Dec 4 '18 at 16:15
thanks for your help!
– mt12345
Dec 4 '18 at 16:15
add a comment |
Thanks for contributing an answer to Mathematics Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3025754%2fjacobi-iteration-in-numerical-methods%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I'm confused. You wrote the code and you do not know where does it control the number of iteration?
– Siong Thye Goh
Dec 4 '18 at 16:14
just didn't know if I had to change both iteration counts
– mt12345
Dec 4 '18 at 16:15