#include <zeroin.h>
Inheritance diagram for ZeroIn< Subject, Ordinate, Abscissa >:

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 |
Whacko, this class does root finding with units intact!
We will be varying the x (Abscissa) value until the y (Ordinate) value is zero.
| Subject | class on which solving of a function like y(x)=0 is to be done | |
| Ordinate | y-value type | |
| Abscissa | x-value type |
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;
|
||||||||||
|
Set the member function that will be solved. The method Ordinate(Subject::*method)(Abscissa) returns values like y(x)
z->setMethod(&Quadratic::evaluate); |
|
||||||||||
|
ZeroIn visit method. This is a visitor pattern implementation of the Brent ZEROIN routine adapted from the C math library version. USAGE
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 |
1.3.8