///////////////////////////////////////////////////////////////////////////
//
// PURPOSE: Test the bisection routine.
// AUTHORS: 1. Rob McEwen, 9 Sept 04
//          2.
//
///////////////////////////////////////////////////////////////////////////
//
#include"MathP.h"
#include <stdio.h>
//
// Roots at +/- 2.0
double func1(double x)
{
   return (x*x - 4.0);
}
//
// From Matlab: 
// fzero('sin(x) - x/2',2)
// ans = 1.8955
double func2(double x)
{
   return( sin(x) - x/2.0);
}

void main()
{
   double x0;
   double x1 = 0.0, x2 = 3.0;
   double tol = 1.0e-4;
   int count[1];

   x0 = Math::bisect( func1, x1, x2, tol, count );
   printf("func1: Interval = [%7.3f, %7.3f], x0 =  %7.3f.\n", x1, x2, x0);
   printf("%d iterations.\n", *count);

   x1 = -3.0; x2 = 0.0;
   x0 = Math::bisect( func1, x1, x2, tol, count );
   printf("func1: Interval = [%7.3f, %7.3f], x0 =  %7.3f.\n", x1, x2, x0);
   printf("%d iterations.\n", *count);

   x1 = -3.0; x2 = 3.0;
   x0 = Math::bisect( func1, x1, x2, tol, count );
   printf("func1: Interval = [%7.3f, %7.3f], x0 =  %7.3f.\n", x1, x2, x0);
   printf("%d iterations.\n", *count);

   x1 = 0.5; x2 = 3.0;
   x0 = Math::bisect( func2, x1, x2, tol, count );
   printf("func2: Interval = [%7.3f, %7.3f], x0 =  %7.3f.\n", x1, x2, x0);
   printf("%d iterations.\n", *count);
}
