title: Error in bisection method code in Matlab
body: <p>I need to write a proper implementation of the bisection method, which means I must address all possible user input errors. Here is my code:</p>
  
  <pre><code>    function [x_sol, f_at_x_sol, N_iterations] = bisection(f, xn, xp, eps_f, eps_x)
      % solving f(x)=0 with bisection method
      %   f is the function handle to the desired function,
      %   xn and xp are borders of search,
      %   f(xn)&lt;0 and f(xp)&gt;0 required,
      %   eps_f defines how close f(x) should be to zero,
      %   eps_x defines uncertainty of solution x
  
      if(f(xp) &lt; 0)
         error('xp must be positive')
      end;
      if(f(xn)&gt;0)
          error('xn must be negative')
      end;
      if (xn &gt;= xp)
          error ('xn must be less than xp')
      end;
  
      xg=(xp+xn)/2; %initial guess
      fg=f(xg); % initial function evaluation
  
      N_iterations=1;
  
      while ( (abs(fg) &gt; eps_f) &amp; (abs(xg-xp) &gt; eps_x) )
          if (fg&gt;0)
              xp=xg;
          else
              xn=xg;
          end
          xg=(xp+xn)/2; %update guess
          fg=f(xg); %update function evaluation
  
          N_iterations=N_iterations+1;
      end
      x_sol=xg; %solution is ready
      f_at_x_sol=fg;
      if (f_at_x_sol &gt; eps_f)
      error('No convergence')
      end
  </code></pre>
  
  <p>and here is the error message I receive when I try to test this in Matlab:</p>
  
  <pre><code>    &gt;&gt; bisection(x.^2, 2, -1, 1e-8, 1e-10)
      Attempted to access f(-1); index must be a positive integer or logical.
  
      Error in bisection (line 9)
      if(f(xp)&lt;0)
  </code></pre>
  
  <p>I was attempting to see if my error codes worked, but it doesn't look like they do. I get the same error when I try to test it on a function that should work.</p>