% bisection code for function f(x)=x^3-x on [0 2]. % I put comments on how I modified the m-file to make it work function m = bisection; a = 0.5; b = 2; n_max = 20; % I define n_max here, so I can change it easily u = a^3-a; v = b^3-b; [ a b u v ] for n=0:n_max c = (a+b)/2; w = c^3-c; sprintf('n: %i - Approximation: %f - Value of function: %f',n,c,w) if ( w*u == 0 ) % I had to put paranthesis around the condition break; % this break ends the if statement that we're in break; % this second one ends the for loop end if ( w*u < 0 ) b = c; v = w; else a = c; u = w; end end