0

MATLAB debugging

I am currently in the debugging process, and I found that this is happening, ruining everything. For reference, f = @(x)ax^3+bx^2+c*x+d (where in this specific case, a = 2, b = -7, c = -8, d = 16).

Also, I notice that temp == -1.6861 returns false..

byInduction
  • 405
  • 1
  • 6
  • 13
  • 1
    I'm guessing it is related to [this question](http://stackoverflow.com/questions/18331068/controlling-the-precision-of-floating-point-number-in-matlab); i.e., `temp` has a lot more significant digits than five that are suppressed by Matlab's default display format. – TroyHaskin Oct 21 '15 at 22:12

1 Answers1

2

This is a typical floating-point problem. Your number is NOT -1.6861, but something close to it. Try format long and then looking at temp. Or even better, try sprintf('%25.20f',temp). temp is the root of your function f as it should be, just not exactly -1.6861.

You actually have to be really careful with this:

(1/3-7/3/7)==0

is false, since

>> (1/3-7/3/7)

ans =

  -5.5511e-17