Finding a root of a function (1-d): Solve for
.
Finding root(s) of N functions of N variables: Solve for
.
Give the algorithm a good starting point.
For 1-d root-finders, provide a bracket for the root.
For the multi-dimensional case, bracketing isn't feasible. The algorithms generally need
Shrink bracket until the root is pinned down, as follows:
Repeatedly "shoot for the zero," using the approximation
Potential problems can occur when far from the root:
(... Draw figures on board, insert in notes later ...)
Important note: should be small enough for your application,
but no smaller. Make sure it is large enough that
is
different from
in your machine's floating point representation.
Use bracketing to protect against any craziness, bisect whenever a Newton's method step fails.
See full example in Numerical Recipes (sec 9.4 in 2nd or 3rd ed.).
We have functions
of
variables
, and want to find the zeros.
There's a great illustration in Numerical Recipes showing why this is hard in general. (Fig. 9.6.1.) If you want to seek the nearest zero to some point, Newton's method should work.
In matrix notation,
using the Jacobian matrix
To find the root, repeatedly solve
Update until convergence
.
Finding a local minimum: find value for which or
is locally minimized. (To find maxima, just
minimize
.)
I will describe one particularly pretty 1-d minimization algorithm that requires no calculation of derivatives and that is guaranteed to find a local minimum.
There are faster algorithms that use derivatives, for 1-d and multi-d problems, including ones that only need the user-supplied function. (These algorithms estimate the derivatives themselves.)
I'll describe the general properties of these, and then we will talk about how to use pre-written implementations.
Similar to bisection, except it starts with a triplet of values
such that
at the middle point is less than at the two ends:
and
.
(... figure ...)
General outline of algorithm:
(*) The point is chosen in the range
or
such that
the updated triplet will have the same proportions as the original triplet.
The ideal ratio turns out to be the golden mean.
Near minimum, to 2nd order,
Terminology: The matrix is called the Hessian.
The vector
is the gradient.
At an extremum, . So we just solve
Essentially the same algorithm as Newton's method works if we know
and
well enough.
Various algorithms differ in how they guard against crazy steps and whether they need the caller to provide a function for the derivatives or Hessian.
What's common among the algorithms:
Numerical Recipes has many different algorithms.
More importantly, there are existing libraries for minimization of functions.
C++ lets you define a new class based on an existing class, like this:
class NewClass : public BaseClass { // usual class definition };
This defines NewClass as having all the same data and member functions as BaseClass, with the following enhancements:
C++ | C |
class MyBase { int i; public: virtual void set_i(int ii); }; class MyClass2 : public MyBase { int j; public: virtual void set_i(int ii); virtual void set_j(int jj); }; |
struct MyBase_s { int i; void (*set_i)(int ii); }; struct MyClass2_s { strict MyBase_s base; int j; void (*set_j)(int jj); }; |
C++ constructor sets virtual function table for new MyClass2 object's set_i and set_j to the correct addresses for MyClass2::set_i() and MyClass2::set_j(). | For any new MyClass2_s variable made, C programmer has to set the values of .set_j and .base.set_i pointers to point to the right functions, and i field must be accessed as base.i. |
See very nice documentation in GNU Scientific Library Reference Manual, section titled "Multidimensional Minimization". http://www.gnu.org/software/gsl/manual/html_node/Multidimensional-Minimization.html
Things to note about how they write documentation:
Things to note about the API:
The best documentation from the authors is at http://root.cern.ch/root/html526/TMinuitMinimizer.html
Get the ROOT examples from the course web page to compile and run.