Getting NaN as my output when working with jacobi iteration

8 views (last 30 days)
I believe it may be something with how I set up my matrices but I am not sure. Also I adapted some of the code from my textbook and I notice in the function they use abserr = norm(X'-P); and P = X';
What does ' do next to the X matrix I am trying to create? If I don't use this symbol I get an error for "matrix dimensions do not agree" when subtracting in the norm function.
Any help would be great.
My output looks like:
x =
NaN NaN NaN
Script:
P = [0;0;0]; %initial guess
tol = 1e-10;
maxiter = 500;
A = [1 -5 -1; 4 1 -1; 2 -1 -6];
B = [-8; 13; -2];
x = jacobi(A,B,P,tol,maxiter)
Function File:
function [X,n] = jacobi(A, B, P, tol, maxiter)
N=length(B);
for n=1:maxiter
for j = 1:N
X(j) = (B(j)-A(j,[1:j-1,j+1:N])*P([1:j-1,j+1:N]))/A(j,j);
end
abserr = norm(X'-P);
relerr = abserr/(norm(X) +eps);
if (abserr <tol) && (relerr<tol)
break;
end
P = X';
end

Answers (1)

Stephen23
Stephen23 on 30 Sep 2014
You should read the documentation for the transpose operator : "Matrix transpose. X' is the complex conjugate transpose of X. X.' is the nonconjugate transpose." This has been covered in plenty of other Answers .
Try it with a vector, and a simple matrix, and you will see the difference.

Community Treasure Hunt

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

Start Hunting!