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
56
|
/* Do a test of the brent 1d root finder */
/*
* Copyright 2000 Graeme W. Gill
* All rights reserved.
*
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
* see the License.txt file for licencing details.
*/
/* Solves (3 - 2*X) * X = -1 */
#include "numlib.h"
int its = 0;
double fcn(void *fdata, double tp);
/* Expected solution */
double expect = -2.80776403408306e-01;
int main(void)
{
int rv;
double x1, x2, soln;
/* Attempt bracket the solution */
x1 = -0.1;
x2 = 0.1;
rv = zbrac(&x1, &x2, fcn, NULL);
if (rv != 0) {
error("zbrack failed with rv = %d\n",rv);
}
printf("Got bracket %f, %f in %d itterations\n",x1,x2,its);
/* Solve the equation */
its = 0;
rv = zbrent(&soln, x1, x2, 1e-6, fcn, NULL);
if (rv != 0) {
error("xbrent failed with rv = %d\n",rv);
}
printf("Solution = %f, expected = %f in %d itterations\n",soln, expect, its);
return 0;
} /* main() */
/* Function being solved */
double fcn(void *fdata, double tp)
{
double temp;
its++;
temp = ((3.0 - 2.0 * tp) * tp) + 1.0;
/* printf("~~ %f returns %f\n",tp,temp); */
return temp;
}
|