Jacobi iterative method in matlab

8 次查看(过去 30 天)
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.

采纳的回答

Mathieu NOE
Mathieu NOE 2021-5-21
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 个评论
Michelle
Michelle 2021-5-22
Thx! Can I get x into 2 x i matrix?
Mathieu NOE
Mathieu NOE 2021-5-25
hello
sorry , I don't understand your question

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by