% Clear variables clear all; % 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 = -cos(xc)-(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