Jacobi iterative method in matlab
12 views (last 30 days)
Show older comments
When A = [ 10 5 ; 2 9 ] and y = [ 6 ; 3 ] and [A][x] = [y], solve x.
start with x1(0) = 0, x2(0) = 0 and continue until abs((x(i+1)-x(i))/x(i)) < e (e = 10^-4)
My code is
clear;
clc;
%%Input
A = [ 10 5 ; 2 9 ];
y = [ 6 ; 3 ];
x = zeros(size(y))
e = 0.0001;
%%Jacobi
D = [ 10 0 ; 0 9 ];
M = inv(D)*(D-A)
x_new = x;
if abs((x_new-x)/x) >= e
x_new = M*x_new + inv(D)*y
end
and it's error says 랭크 부족, rank = 0, tol = 0.000000e+00.
0 Comments
Accepted Answer
Mathieu NOE
on 21 May 2021
hello
there were quite a few bugs there - now see the correction below
you should do a while and not a for loop if you stop criteria is based on a error
inside the loop , the update logic was wrong and incomplete
also fixed a few initialization issues
clear;
clc;
%%Input
A = [ 10 5 ; 2 9 ];
y = [ 6 ; 3 ];
x = zeros(size(y));
e = 0.0001;
%%Jacobi
% D = [ 10 0 ; 0 9 ];
D = diag(diag(A));
invD = inv(D);
M = invD*(D-A);
x_new = x;
err = 1; % put a value here greater than e otherwise the while loop will not start
while err >= e
x_new = M*x + inv(D)*y; % update x_new
err = norm((x_new-x)./(x+eps)) % compute error (sue norm)
x = x_new; % update x based on x_new
end
x
2 Comments
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!