Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

ZeroIn< Subject, Ordinate, Abscissa > Class Template Reference

Root-finding by Brent algorithm. More...

#include <zeroin.h>

Inheritance diagram for ZeroIn< Subject, Ordinate, Abscissa >:

DesignByContract List of all members.

Public Member Functions

void setLowerBound (const Abscissa &ax)
 Set the lower bound of the range that will be searched for a solution.
Abscissa getLowerBound () const
 setLowerBound
void setUpperBound (const Abscissa &bx)
 Set the upper bound of the range that will be searched for a solution.
Abscissa getUpperBound () const
 setUpperBound
void setTolerance (const Abscissa &tol)
 set tolerance of the solution
void setMethod (Ordinate(Subject::*method)(const Abscissa &))
 Set the member function that will be solved.
void visit (Subject *subject)
 ZeroIn visit method.
bool isSolved (void) const
bool isSolved (const Ordinate &error)
Abscissa getSolution (void) const
Ordinate getError (void) const

Detailed Description

template<class Subject, class Ordinate = double, class Abscissa = double>
class ZeroIn< Subject, Ordinate, Abscissa >

Root-finding by Brent algorithm.

Whacko, this class does root finding with units intact!

We will be varying the x (Abscissa) value until the y (Ordinate) value is zero.

Parameters:
Subject class on which solving of a function like y(x)=0 is to be done
Ordinate y-value type
Abscissa x-value type
See also:
units.h

ZeroIn::visit

To solve the quadratic x²-4x-4=0 in the region x in [-10,4.566] by iteration (
See also:
zeroin.test.cpp):
                        class Quad{
                                double eval(double x){
                                        return x*x-4*x-4;
                                }
                        };

                        // ...

                        Quad q;

                        ZeroIn<Quad> z;
                        z.setLowerBound(-10);
                        z.setUpperBound(4.566);
                        z.setMethod(&ZeroInTest::eval);
                        z.setTolerance(1e-10);
                        z.visit(q);

                        if(z.isSolved(0.001)){
                                cerr << "Quadratic was solved, x = " << z.getSolution() << endl;
                        }

                        cerr << "Unable to solve quadratic over given region" << endl;


Member Function Documentation

template<class Subject, class Ordinate = double, class Abscissa = double>
void ZeroIn< Subject, Ordinate, Abscissa >::setMethod Ordinate(Subject::*  method)(const Abscissa &)  )  [inline]
 

Set the member function that will be solved.

The method Ordinate(Subject::*method)(Abscissa) returns values like y(x)

                                z->setMethod(&Quadratic::evaluate);

template<class Subject, class Ordinate = double, class Abscissa = double>
void ZeroIn< Subject, Ordinate, Abscissa >::visit Subject *  subject  )  [inline]
 

ZeroIn visit method.

This is a visitor pattern implementation of the Brent ZEROIN routine adapted from the C math library version.

USAGE

See also:
brent.shar at http://www.netlib.org/c/
Original documentation:

function ZEROIN - obtain a function zero within the given range

Input double zeroin(ax,bx,f,tol) double ax; Root will be sought for within a range [ax,bx] double bx;

double (*f)(double x); Name of the function whose zero will be sought for

double tol; Acceptable tolerance for the root value. May be specified as 0.0 to cause the program to find the root as accurate as possible

Output Zeroin returns an estimate for the root with accuracy 4*EPSILON*abs(x) + tol

Algorithm

G.Forsythe, M.Malcolm, C.Moler, Computer methods for mathematical computations. M., Mir, 1980, p.180 of the Russian edition

The function makes use of the bissection procedure combined with the linear or quadric inverse interpolation.

At every step program operates on three abscissae - a, b, and c.

b - the last and the best approximation to the root a - the last but one approximation c - the last but one or even earlier approximation than a that 1) |f(b)| <= |f(c)| 2) f(b) and f(c) have opposite signs, i.e. b and c confine the root

< Distance from the last but one to the last approximation

< Actual tolerance

< Interpolation step is calculated in the form p/q; division

< operations is delayed until the last moment

< Step at this iteration


The documentation for this class was generated from the following file:
Generated on Tue Mar 22 19:07:07 2005 for freesteam by doxygen 1.3.8