필터 지우기
필터 지우기

error in while loop

조회 수: 1 (최근 30일)
Piyanut Nanta
Piyanut Nanta 2015년 6월 17일
편집: Nicholas Sullivan 2015년 6월 19일
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