'Hw 2, Computational Problem (a): Bisection'

clear all

a=0
b=1
iteration = 0;
nmax = 10;

ptrue = atan(6)/pi

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

while ( iteration <= nmax )
   
   iteration = iteration + 1;
  
   fa = tan(pi*a)-6;
   fb = tan(pi*b)-6;
   fp = tan(pi*p)-6;
   
   if (fa*fp > 0)
       a = p;
   else
       b = p;
   end
  
   p = a + (b-a)/2;
   fp = tan(pi*p)-6;
   
   fprintf(1,'iteration # %3d ',iteration);  fprintf(1,', p = %15.8e',p);
   fprintf(1,', |p-ptrue| = %15.8e',abs(p-ptrue));
   fprintf(1,', f(p) = %15.8e', fp);  fprintf(1,'\n');

end