function bis = bisec(f,a,b,n) list = []; for k=1:n [a,b] x = (a+b)/2; list = [list;x]; if f(x)==0; bis=x; break else if f(x)*f(a)<0 b=x; else if f(x)*f(b)<0 a=x; end end end end % error = abs(list-1); % comparison with the exact result (in this example it's equal to 1) % semilogy(1:n,error,'r'); % error plot bis=x;