What’s my error ?!

1 个评论

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 个评论

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 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by