error in while loop

1 次查看(过去 30 天)
Piyanut Nanta
Piyanut Nanta 2015-6-17
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

回答(1 个)

Nicholas Sullivan
Nicholas Sullivan 2015-6-17
编辑:Nicholas Sullivan 2015-6-19
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 个评论
Walter Roberson
Walter Roberson 2015-6-18
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 2015-6-19
Thanks for the correction. I wasn't paying attention.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by