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 CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by