error in while loop

2 views (last 30 days)
Piyanut Nanta
Piyanut Nanta on 17 Jun 2015
Edited: Nicholas Sullivan on 19 Jun 2015
Hi, I want to loop of Jacobi method but error in while loop. help me please
clear all
clc
n=input('Number of equations (n): ');
A=input('Enter the matrix A,2D Matrix: ');
b=input('Enter the matrix b,Column vector: ');
E=input('Permissible error: ');
x0=zeros(1,n);
x=x0;
k=0;
while
k=k+1;
fprintf('%d',k);
for i=1:n
j=1:n
if i~=j
sum=sum+A(i,j)*x(j);
end
x(i)=(b(i)-sum)/A(i,i);
fprintf('%10.5f',x(i));
end
end

Answers (1)

Nicholas Sullivan
Nicholas Sullivan on 17 Jun 2015
Edited: Nicholas Sullivan on 19 Jun 2015
I think you might be missing a nested 'for' statement. Instead of
for i=1:n
j=1:n
Write
for i=1:n
for j=1:n
and then add an extra 'end' statement where appropriate.
  2 Comments
Walter Roberson
Walter Roberson on 18 Jun 2015
k will never test equal to k+1 unless k is infinity.
The code was not "while k=k+1", it just looked like that because it was not formatted. Instead it had a 'while" with nothing else on the same line. That needs to be changed to something like
while true
That would create an infinite loop. Possibly along the way some test involving E (permissible error) is intended. The way to calculate the current error is not documented here, though, so no suggestions on that.
Nicholas Sullivan
Nicholas Sullivan on 19 Jun 2015
Thanks for the correction. I wasn't paying attention.

Sign in to comment.

Categories

Find more on MATLAB 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!