% Solution to Hw1, Computational Problem 2.

'Computational Problem 2 start'

tol = 0.00001;
err = 1;
nmax = 100;

% input 
a1=0;
b1=1;
%a2 = 0;
%b2 = 1;

a=a1;
b=b1;

% iterates start here.

itnum = 0;
p = a + (b-a)/2;

while ( (err > tol) & (itnum < nmax) )
   itnum = itnum + 1 
  
   fa = exp(a) - a*a + 3*a - 2;
   fb = exp(b) - b*b + 3*b - 2;
   fp = exp(p) - p*p + 3*p - 2;
   
   if (fa*fp > 0)
       a = p;
   else
       b = p;
   end
  
   pold = p; 
   p = a + (b-a)/2
   
   rate = abs(p - pold)
   err = abs(p-pold); %/abs(p)

end

  fprintf(1,'result after iteration number %5d ',itnum);
  fprintf(1,'is %15.7e \n',p);
  fprintf(1,'f(p) = %5d \n \n', exp(p) - p*p + 3*p - 2);

  'Computational Problem 2 end'
