function x = bisect(f, int, tol)
% bisect - bisection algorithm for the scalar equation f(x) = 0
% 
%   Syntax: 
%           x = bisect(f, int, tol)
%   Arguments: 
%           f - function handle pointing to a function file  
%           int - 1x2 matrix specifying an interval int = [a,b]
%           tol - a tolerance 
%   Returns: 
%           x - an approximate solution in the interval int = [a,b]
%   Description:
%           The program bisect uses the bisection algorithm to 
%           compute an approximate solution of the scalar equation
%           f(x) = 0 in the interval int = [a,b].  The function file must 
%           contain the function y = f(x).  The function values f(a) 
%           and f(b) must have opposite signs.  Then the program
%           computes an approximate solution x with the error
%           |x-x_exact| < tol.  It returns an empty value for x if
%           f(a) and f(b) have the same sign.  
%   Examples: 
%          x = bisect(@sin,[1,4],1e-7)   computes pi to 7 decimals
%
%----------------------------------------------------------------------

a=int(1);                  % left interval endpoint 
b=    ??;                  % right endpoint

fa=f(a);             % evaluate f at a
fb=  ??;             % evaluate f at b

% Extra tasks 1,2,3. Do these only after the main task. 

% Extra task 1: add code here to interchange a and b in case a>b.

% Extra task 2: add code here that displays an appropriate message, and then stops
% and returns x = [] if fa and fb do not have opposite signs

% Extra task 3: add code here that displays an appropriate message and stops if the
% given tolerance is not positive.

% Main task:  replace the ??'s to get a working bisection solver
% test the solver on problems with known solution to ensure that it works.
 
while b-a ??  

  x=??;             % compute midpoint x of (current) interval [a b]
  fx=??;               % evaluate f at midpoint

  if fx==0
    return;
  end 

  if fa*fx<0               % change of sign in the left half 
    b=??;                  % make left half the new current interval
    fb=??
  else
    ??                     % make right half the new current interval 
  end

end

x=??   % compute the midpoint one more time 

