1 |
francois |
283 |
//--------------------------------------------------------------------------- |
2 |
|
|
|
3 |
|
|
#ifndef ot_root_findH |
4 |
|
|
#define ot_root_findH |
5 |
|
|
//--------------------------------------------------------------------------- |
6 |
|
|
|
7 |
|
|
#include <math.h> |
8 |
|
|
#include <stdio.h> |
9 |
|
|
|
10 |
|
|
#ifdef WINDOWS_VERSION |
11 |
|
|
#ifdef BUILT_DLL_OUTIL |
12 |
|
|
#define DLLPORTOUTIL __declspec(dllexport) |
13 |
|
|
#else |
14 |
|
|
#define DLLPORTOUTIL __declspec(dllimport) |
15 |
|
|
#endif |
16 |
|
|
#else |
17 |
|
|
#define DLLPORTOUTIL |
18 |
|
|
#endif |
19 |
|
|
|
20 |
foucault |
27 |
class DLLPORTOUTIL OT_ROOT_FIND_1D { |
21 |
|
|
public: |
22 |
francois |
283 |
|
23 |
foucault |
27 |
typedef double (*Function)(double,void*); |
24 |
|
|
|
25 |
francois |
283 |
/** ZeroBracOut (fn, x1, x2) |
26 |
|
|
given a function fn and an initial guessed range [x1, x2], this routine |
27 |
|
|
expands the range geometrically until a root is bracketed by x1 & x2, in |
28 |
|
|
which case it returns 1, or the range becomes too large, returning 0 */ |
29 |
|
|
static unsigned ZeroBracOut (Function fn, double &x1, double &x2, void* pvData = 0); |
30 |
|
|
static unsigned ZeroBracOut (Function fn, double xStart, double xEnd, double &x1, double &x2, void* pvData); |
31 |
foucault |
27 |
|
32 |
francois |
283 |
/** ZeroBracIn (fn, x1, x2, n, xb1, xb2, nb) |
33 |
|
|
given a function fn defined on [x1, x2], subdivide the interval into n |
34 |
|
|
equally spaced segments & search for zero crossings of the function. nb is |
35 |
|
|
input as the maximum number of roots sought. output is the number of |
36 |
|
|
bracketing pairs xb1[], xb2[] that are found. */ |
37 |
|
|
static unsigned ZeroBracIn (Function fn, const double &x1, const double &x2, |
38 |
|
|
unsigned n, double xb1[], double xb2[], unsigned nb, void* pvData = 0); |
39 |
foucault |
27 |
|
40 |
francois |
283 |
/** RootBisect (fn, x1, x2, xacc) |
41 |
|
|
using bisection, find the root of fn known to lie between x1 and x2. the |
42 |
|
|
returned root will be refined until its accuracy is +/- xacc. */ |
43 |
foucault |
27 |
|
44 |
francois |
283 |
static double RootBisect (Function fn, const double &x1, const double &x2, const double &xacc, void* pvData = 0); |
45 |
foucault |
27 |
|
46 |
francois |
283 |
/** RootFlsPos (fn, x1, x2, xacc) |
47 |
|
|
using the false position method, find the root of fn in [x1, x2] to an |
48 |
|
|
accuracy of +/-xacc. */ |
49 |
foucault |
27 |
|
50 |
francois |
283 |
static double RootFlsPos (Function fn, const double &x1, const double &x2, |
51 |
|
|
const double &xacc, void* pvData = 0); |
52 |
foucault |
27 |
|
53 |
francois |
283 |
/* RootNewton (fn, df, x1, x2, xacc) |
54 |
|
|
using the newton-raphson method, find the root of fn in [x1, x2] to an |
55 |
|
|
accuracy +/-xacc. df gives the 1st derivative of fn. */ |
56 |
foucault |
27 |
|
57 |
francois |
283 |
static double RootNewton (Function fn, Function df, |
58 |
|
|
const double &x1, const double &x2, const double &xacc, void* pvData = 0); |
59 |
foucault |
27 |
|
60 |
francois |
283 |
/* RootSafe (fn, df, x1, x2, xacc) |
61 |
|
|
using a combination of newton-raphson and bisection, find the root of fn |
62 |
|
|
in [x1, x2] to an accuracy +/-xacc. df gives the 1st derivative of fn. */ |
63 |
|
|
|
64 |
|
|
static double RootSafe (Function fn, Function df, |
65 |
|
|
const double &x1, const double &x2, const double &xacc, void* pvData = 0); |
66 |
|
|
}; |
67 |
|
|
#endif |