% Solution to Hw1, Computational Problem 3.

'Computational Problem 3 start'

tol = 0.0001;
err = 1;
nmax = 100;

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

a=a1;
b=b1;

ptrue = sqrt(3);

% iterates start here.

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

while ( (err > tol) & (itnum < nmax) )
   itnum = itnum + 1 
  
   fa = a*a - 3;
   fb = b*b - 3;
   fp = p*p - 3;
   
   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

difference = abs(ptrue - p)

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

'Computational Problem 3 end'
