Jacobi iterative method in matlab

7 Ansichten (letzte 30 Tage)
Michelle
Michelle am 20 Mai 2021
Kommentiert: Mathieu NOE am 25 Mai 2021
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.

Akzeptierte Antwort

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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Programming finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by