Jacobi iterative method in matlab

8 ビュー (過去 30 日間)
Michelle
Michelle 2021 年 5 月 20 日
コメント済み: Mathieu NOE 2021 年 5 月 25 日
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 Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by