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)

Categories

Find more on Operators and Elementary Operations 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!