1 Comment

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).

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 10 Mar 2020
Edited: Adam Danz on 11 Mar 2020

0 votes

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 Comments

There are an unknown number of iterations, so you cannot use a for loop.
Adam Danz
Adam Danz on 11 Mar 2020
Edited: Adam Danz on 11 Mar 2020
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).

Sign in to comment.

Asked:

on 10 Mar 2020

Edited:

on 11 Mar 2020

Community Treasure Hunt

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

Start Hunting!