What’s my error ?!
Show older comments

1 Comment
Adam Danz
on 10 Mar 2020
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).
Answers (1)
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
Walter Roberson
on 11 Mar 2020
There are an unknown number of iterations, so you cannot use a for loop.
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!