Debian Codegraph @

easel/esl_rootfinder.tex

raw
1
2
3
4 \subsection{Example of using the rootfinder API}
5
6 To use the bisection method, you provide a function that computes
7 $f(x)$, and you provide an initial bracket $(x_L..x_R)$ in which the
8 root is known to lie. An example of using the bisection method to
9 compute a root of the quadratic function $ax^2 + bx + c = 0$ for
10 $a=5$, $b=2$ and $c=-1$ (note that this function has two roots, one
11 $<0$ and one $>0$; the bracket $0..100$ makes the example find only
12 the positive root):
13
14 \input{cexcerpts/rootfinder_example}
15
16
17 To use the Newton/Raphson method, you provide a function that computes
18 $f(x)$ and its first derivative $df(x)/dx$, and you provide an initial
19 guess for the root $x$. An example of using the Newton/Raphson method
20 to compute the root of the same function above (which has a derivative
21 $df(x)/dx = 2ax + b$) is:
22
23 \input{cexcerpts/rootfinder_example2}
24
25 In this example, because the initial guess was negative, the other
26 root gets found.
27
28 Currently, just these two rootfinding algorithms are implemented. The
29 bisection method does not require derivative information, and it
30 requires the caller to provide an interval $(x_L..x_R)$ in which the
31 root lies ($f(x_L)$ and $f(x_R)$ have opposite signs). Newton/Raphson
32 uses derivative information, and it only needs an initial guess for
33 $x$, not an interval. Thus there are two different \ccode{\_Create*()}
34 routines, \ccode{esl\_rootfinder\_CreateBracketer()} for initializing
35 a bisection method, and \ccode{esl\_rootfinder\_CreatePolisher()} for
36 initializing a Newton/Raphson method. The reason for the more general
37 names (\ccode{CreateBracketer()} and \ccode{CreatePolisher()} is that
38 I expect other rootfinding algorithms (if we ever implement any) will
39 group similarly: bracketing methods without derivative information,
40 and ``polishing'' methods that use derivative information. But this
41 may be misguided, and may change in the future.
42
43