Fortunately, when a function, like
above, is differentiable,
more efficient optimization algorithms can be used. If minimize()
is given the differential of the optimized function, using the "df"
option, it will use a conjugate gradient method.
-
- ## Function returning partial derivatives
function dc = diffoo (x)
x = x(:)' - 1;
dc = sin (x) + 2*x/9;
endfunction
[x, v, n] = minimize ("foo", x0, "df", "diffoo")
This produces the output :
-
- x =
1.00000 1.00000 1.00000
v = -3
n =
108 6
The same minimum has been found, but only 108 function evaluations
were needed, together with 6 evaluations of the differential. Here,
diffoo() takes the same argument as foo() and returns
the partial derivatives of
with respect to the corresponding
variables. It doesn't matter if it returns a row or column vector
or a matrix, as long as the
element of diffoo(x)
is the partial derivative of
with respect to
.
Søren Hauberg
2008-04-29