The method of conjugate-gradients (Numerical Recipes, Sec. 10.6) can make dramatic improvements in the convergence.
While the theory behind conjugate-gradients is subtle, the
implementation is straight-forward. The theory states that at step n, rather
than choosing the search direction (
) to be along the down-hill gradient
one should search along a direction which is a linear
combination of the previous search direction (
) and the down-hill
gradient,
The ``magic'' linear-combination coefficient
is
Because conjugate-gradients depends upon being relatively near a minimum, it pays to first do a few (say ten) standard steps of line-minimization first. Thus, you should replace the statement
in your code with the following code fragmentdir=(-1.)*grad;
if (i<=10)
dir=(-1.)*grad;
else {
beta= ...
dir=beta*dir+(-1.)*grad;
}
where beta is defined as above.