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..
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
temphas a lot more significant digits than five that are suppressed by Matlab's default display format.