Hi, my code doesn't seem to continue beyond the first iteration of the bisection method in my loop. I also am not getting the right answer. I'm not sure where the mistake is.
function c = findroot(f,a,b,e)% a is the initial value
% b is the final value
% e is the tolerance
if f(a)*f(b)>0 error('f(a) and f(b) must have opposite signs')endc = (a+b)/2;while abs(b-c) <= e; f = f(c); break if f(a)*f(c) <= 0; c = b; f(c) = f(b); else c = a; f(c) = f(a); endendend
I am calling the following in the command window:
>> f = @(x) x.^2-x-1;
>> findroot(f, 1, 2, 0.001)
Best Answer