% given a & b such that f(a)<0 & f(b)>0 % ep is the error tolerance % max_iterate is the maximum of the iterative % steps a = 1; b = 2; ep = 10^-4; max_iterate = 10000; % check whether the interval is in the right form or not if a >= b disp('a < b is not true. Stop!') return end % check the sign of the initial boundary points fa = f(a); fb = f(b); if sign(fa)*sign(fb) > 0 disp('f(a0) and f(b0) are of the same sign. Stop!') return end % produce midpoint of boundary points c = (a+b)/2; % it_count is the iteration steps it_count = 0; while b-c > ep & it_count < max_iterate % iteration condition it_count = it_count + 1; fc = f(c); % check the sign of the generated point if sign(fb)*sign(fc) <= 0 a = c; fa = fc; else b = c; fb = fc; end c = (a+b)/2; end format long root = c format short e error_bound = b-c format short it_count