댓글 수: 1

Adam Danz
Adam Danz 2020년 3월 10일
This is better than the 1-line of code you had previously but an image of code makes it difficult for us to work with since we can't copy-paste it.
Since I already copyied your previous line of code, here's what it should look like when formatted properly.
i=1;
x(i)=5;
error(i)=9999;
while error (i) >=(0.5)
X(i+1)=((x(i)^2-2.5)/(1.8));
error(i+1)=abs((((x(i+1)-x(i))/(x(i+1)))*1000));
i=i+1;
end
To use smart-indentation, highlight all of your code (ctrl+A, in Window, followed by ctrl+i).

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

답변 (1개)

Adam Danz
Adam Danz 2020년 3월 10일
편집: Adam Danz 2020년 3월 11일

0 개 추천

One reason it's difficult to see the error is because all of your code is in 1 line. There's no advantage to doing this. Disadvantages are
  • Really diffcult to read
  • Error messages aren't helpful in pointing to the error
  • It's realy difficult to see what parts of the code are in loops, etc
Your code contains a variable X (upper case) which should probably be x (lower case).
Also, don't use error as a variable name since error() is a common function.
You should also replace the while-loop with a for-loop if i is an interger starting with 1 that increases incrementally.

댓글 수: 2

Walter Roberson
Walter Roberson 2020년 3월 11일
There are an unknown number of iterations, so you cannot use a for loop.
Adam Danz
Adam Danz 2020년 3월 11일
편집: Adam Danz 2020년 3월 11일
When I run the code, after replaceing X with x, on the 10th iteration x equals inf and error equals NaN which ends the while-condition. It stops after 10 iterations (when i equals 11).

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

카테고리

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

질문:

2020년 3월 10일

편집:

2020년 3월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by