%% Jacobi Method
%% Solution of x in Ax=b using Jacobi Method
% * Initailize 'A' 'b' & intial guess 'x' %% A=[2.08 -1 0 0;-1 2.08 -1 0;0 -1 2.08 -1;0 0 -1 2.08]; b=[200.8;0.8;0.8;40.8]'
b=[200.8;0.8;0.8;40.8]'
x=[0 0 0 0]'
n=size(x,1);
normVal=Inf;
%%
% * Tolerence for method
tol=1e-5;
itr=0;
%% Algorithm: Jacobi Method
%%
while normVal>tol
xold=x;
for i=1:n
sigma=0;
for j=1:n
if j~=i
sigma=sigma+A(i,j)*x(j);
end
end
x(i)=(1/A(i,i))*(b(i)-sigma);
end
itr=itr+1;
normVal=abs(xold-x);
end
%%
fprintf('Solution of the system is : \n%f\n%f\n%f\n%f in %d iterations',x,itr);

댓글 수: 4

DGM
DGM 2021년 5월 18일
I have no idea. If you want someone to debug your code, present it in a format that can actually be tested.
I don't know what's wrong with it. After much reformatting, it runs without errors. If the results aren't what you expect, I wouldn't know. You didn't describe the problem or mention any errors.
Walter Roberson
Walter Roberson 2021년 5월 18일
I had to guess about the spacing when I reformatted it to make it readable.
DGM
DGM 2021년 5월 18일
Are these occasional imploded posts a consequence of some issue with char encoding and user locale?
Walter Roberson
Walter Roberson 2021년 5월 18일
I don't think so. You get bad formatting like that if you post code on the desktop without using the > button to create acode region, as the site supposes you have typed in conversational text.
(If you happen to post code on mobile, then the result depends upon whether the first character of the paragraph is space or not; if it is then the paragraph up to the next empty line is formatted as code.)

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2021년 5월 17일

댓글:

2021년 5월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by