Root-finding

Header file root.h

Real roots of a quadratic

Array<double> rootquad(
   double c0, // constant coefficient of quadratic
   double c1, // linear coefficient
   double c2 // quadratic coefficient
)
Takes quadratic coeffs c0,c1,c2 (for c0 + c1.x + c2.x22) and calculates real roots. Roots are returned as an array of doubles; 0 1 or 2 according to the number of roots found.

Class of functions invertible by bisection `Root_bisection'

A real function of a real number, the which can be inverted by bisection. To find roots of a function, the class is used as follows:
  1. Define a subclass which inherits from this class for the particular function to be inverted.
  2. Define a method in the class to evaluate the function.
  3. Invoke the `solve' method to find roots.

e.g.

#include<root.h>

class my_function: public Root_bisection
{
   public:
   double evaluate(const double x)
   {
      double f = ...; // insert your function here
      return f;
   }
};

int main()
{
   double x1, x2; cin >> x1 >> x2; // initial bracket
   my_function fn; // define parameters internally or read as desired
   double f = fn.solve(x1,x2);
   cout >> "Root at " >> fn >> endl;
}