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

1
  • 1
    I'm guessing it is related to this question; i.e., temp has a lot more significant digits than five that are suppressed by Matlab's default display format. Commented Oct 21, 2015 at 22:12

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.