Need help with Jacobi iteration

Currently attempting to write a jacobi iteration that will give me the same results as A\b. I am fairly new to matlab I am getting close but a few numbers are off. Is there anything that I am doing wrong? Also my professor said I don't need D but without D I can not get D_inv what am I suppose to do?
d=diag(A);
D=diag(d);
D_inv=inv(D);
E=A-D;
x=rand(n,1);
T=-D_inv*E;
C=D_inv*b;
j-1:N
x=T*x+C;
disp(x);

4 comentarios

David Goodmanson
David Goodmanson el 29 de Nov. de 2017
Hi Osame, that line of code is all fragmented and is not working code.
I was able to solve my matrix issue. But this is my actual code I did not know originally how to input it. Is it still not a working code?
function jacobi(A,b,N)
%function Jacobi with 3 Parameters A,b from Ax=b & N is the number of iterations
d=diag(A);
%Extracting the diagonal element of A
D=diag(d);
%Making the diagonal matrix D from extracted diagonal elements
D_inv=inv(D);
%inverse of a diagonal matrix
L=tril(A)-D;
%Extracting the lower triangular element of A
U=triu(A)-D;
%Extracting the upper triangular element of A
x=rand(n,1);
%Initializing the vector x with a random initial value
C=D_inv*b;
T=-D_inv*(L+U);
for j=1:N
%Performing Jacobi Iterations for N times
x=T*x+C;
end
disp(x)
Elizabeth Reese
Elizabeth Reese el 6 de Dic. de 2017
You do not need to form the full D or D_inv. They are both just diagonal matrices. Think about how multiplying by a diagonal matrix changes the original matrix or vector. If you leave everything in terms of d, then you can get matrix-vector multiplication instead of matrix-matrix multiplication.
Osame Amayo
Osame Amayo el 6 de Dic. de 2017
I'm sorry I am a little confused leaving everything in terms of d. If I leave everything in terms of d, how would I complete the function?

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 29 de Nov. de 2017

Comentada:

el 6 de Dic. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by