Im getting NaN as output Help pls
Show older comments
Im trying to get the answer but I get NaN
function x= numjacobi(A,b,x0,tol,trace)
[nrow, ncol]=size(A);
%
if nrow~=ncol
errordlg('cant calculate');
end
%
if nargin<3
x0=ones(size(A,1),1);
end
%
if nargin<4
x0=ones(size(A,1),1);
end
%
if nargin<5 || isempty (tol)
tol= 1e-6;
end
b= (b(:).')';
x0=(x0(:).')';
n=length(b);
if n~=nrow
errordlg('b has different lenght than A');
end
%
if det(A)==0
fprintf('Error')
end
%
if nargin<5
trace=0;
end
%% Matrix with modified coefficients and vector with modified coefficients
D = diag(diag(A));
a0 = (inv(D)*A)-eye(nrow);
b0 = inv(D)*b;
x=x0;
x0 = x + 2*tol;
iter=0;
%%
while max(abs(x-x0))>=tol || iter<100
x0=x;
x=b0-(a0)*x0;
iter=iter+1;
if trace
fprintf('\n Iteraction no %3d\n', iter)
fprintf('%8.6g ',x)
end
end
end
%%%%%
% AND IM USING THIS VECTORS AND CALL THE FUNCTION
A=[80,2,1;60,-1,1;50,0,-1];
b=[536;678;907];
x0=[1 1 1]';
answ=numjacobi(A,b,x0,1e-6,0);
disp(answ)
Answers (1)
Cris LaPierre
on 13 Mar 2021
Edited: Cris LaPierre
on 13 Mar 2021
0 votes
The value of x in your while loop keeps getting bigger and bigger.
At iter==1610, x = [2.6071; -Inf; -Inf] * 1.0e+306
At iter==1611, x = [Inf; NaN; NaN]
At iter==1612, x = [NaN; NaN; NaN]
There appears to be something incorrect about your calculation of x.
1 Comment
Blue Matlover
on 13 Mar 2021
Categories
Find more on NaNs in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!