Why is the code dont stoping when 'x' is equal to 10?

 Accepted Answer

You need to know that it‘s because of floating-point error because .1 is not exactly .1 to check
sprintf('%.32f',.1) % you will see numbers after the decimal Point
Search with the tag floating-point in this forum you will find lot of discussions. You need to add tolerance to your while loop:
while abs(x-10)>1e-4
Note: You don‘t need a loop to do this task, it can be vectorised.
x = .1:.1:10

More Answers (0)

Community Treasure Hunt

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

Start Hunting!