[go: up one dir, main page]

Menu

[r353]: / libpetey / supernewton.h  Maximize  Restore  History

Download this file

57 lines (46 with data), 1.6 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#ifndef SUPERNEWTON_H_INCLUDED
#define SUPERNEWTON_H_INCLUDED 1
namespace libpetey {
//complicates things somewhat, but we need to know the both
//the number of iterations and the number of bisection steps
//and to separate these from the error status:
struct supernewton_stat {
int niter; //total number of iterations
int nbis; //number of bisection steps
int code; //error status
};
//root-finding function which brackets the root
//and then approximates root by fitting a third-
//order polynomial
template <class real>
real supernewton(void (*funcd) (real, void *, real *, real *),
void *params, //function parameters
real x1, //first bracket
real x2, //second bracket
real xtol, //desired (absolute) tolerance
real ytol,
long maxiter, //maximum number of iterations
supernewton_stat *err, //returns error status
//returns 0 or less for failure
real &y1, //to avoid redundant calculation
real &dydx1, //returns final values in these two
real y2,
real dydx2);
template <class real>
inline real supernewton(void (*funcd) (real, void *, real *, real *),
void *params, //function parameters
real x1, //first bracket
real x2, //second bracket
real xtol, //desired (absolute) tolerance
real ytol,
long maxiter, //maximum number of iterations
supernewton_stat *err) //number of iterations/error code
{
real y1, y2, dydx1, dydx2;
(*funcd) (x1, params, &y1, &dydx1);
(*funcd) (x2, params, &y2, &dydx2);
return supernewton(funcd, params, x1, x2, xtol, ytol, maxiter, err,
y1, dydx1, y2, dydx2);
}
}
#endif