% input handle of function that want to find root of % output is root function root = bisection_function(function_to_find_root_of) % Define our starting interval xl=-2 ; xr=1; % Start of the loop, note that we have decided that 1000 iterations is too % many, this is fairly arbitrary and can be changed for j=1:1000 xc = (xr+xl) / 2 ;% Find the midpoint of the interval fc = function_to_find_root_of(xc) ;% Calculate the function at the midpoint % Check to see if we should go left or right if ( fc>0 ) xl=xc; else xr=xc; end % Check to see if we are close to the 0 of the function if ( abs(fc)<10^(-5) ) break end end % Display the results xc ; fc ; root=xc ;