function [ x, e ] = secant1( f,l,r,n ) %Bisection_1 calculates root of function between l and r with n iterations % output: approximate value x, estimated error e fl = f(l); fr = f(r); res = [NaN, l, r, fl, fr, r-l]; if fl*fr > 0.0 error('Function has same sign at both initial end points l and r \n'); end % Main loop of algorithm for i = 1:n x = l - (r-l)/(fr-fl) *fl; y = f(x); if y == 0.0 e = 0; break end if fl*y > 0 % Trick: check if same sign! l = x; fl = y; else r = x; fr = y; end e = (r-l)/2; % Rough estimate for error res = [res; x, l, r, fl, fr, e]; end ROWS = [0:n]; printmat(res, 'Results', num2str(ROWS), 'x l r fl fr e'); plot([0:n], log(res(:,6))); end