Optimization Toolbox | ![]() ![]() |
Nonlinear Equations with Finite-Difference Jacobian
In the preceding example, the function bananaobj
evaluates F
and computes the Jacobian J
. What if the code to compute the Jacobian is not available? By default, if you do not indicate that the Jacobian can be computed in the objective function (using the Jacobian
parameter in options
), fsolve
, lsqnonlin
, and lsqcurvefit
instead use finite differencing to approximate the Jacobian. This is the default Jacobian option.You can select finite differencing by setting the Jacobian
parameter to 'off'
in optimset
.
This example uses bananaobj
from the preceding example as the objective function, but sets the Jacobian
parameter to 'off'
so that fsolve
approximates the Jacobian and ignores the second bananaobj
output. It accepts the fsolve
default 'off'
for the LargeScale
parameter, and the default nonlinear equation medium-scale algorithm 'dogleg'
:
n = 64; x0(1:n,1) = -1.9; x0(2:2:n,1) = 2; options=optimset('Display','iter','Jacobian','off'); [x,F,exitflag,output,JAC] = fsolve(@bananaobj,x0,options);
The example produces the following output:
Norm of First-order Trust-region Iteration Func-count f(x) step optimality radius 0 65 4281.92 615 1 1 130 1546.86 1 329 1 2 195 112.552 2.5 34.8 2.5 3 260 106.24 6.25 34.1 6.25 4 261 106.24 6.25 34.1 6.25 5 326 51.3854 1.5625 6.39 1.56 6 327 51.3854 3.90625 6.39 3.91 7 392 43.8722 0.976562 2.19 0.977 8 457 37.0713 2.44141 6.27 2.44 9 458 37.0713 2.44141 6.27 2.44 10 523 26.2485 0.610352 1.52 0.61 11 588 20.6649 1.52588 4.63 1.53 12 653 17.2558 1.52588 6.97 1.53 13 718 8.48582 1.52588 4.69 1.53 14 783 4.08398 1.52588 3.77 1.53 15 848 1.77589 1.52588 3.56 1.53 16 913 0.692381 1.52588 3.31 1.53 17 978 0.109777 1.16206 1.66 1.53 18 1043 0 0.0468565 0 1.53 Optimization terminated successfully: First-order optimality is less than options.TolFun
The finite-difference version of this example requires the same number of iterations to converge as the analytic Jacobian version in the preceding example. It is generally the case that both versions converge at about the same rate in terms of iterations. However, the finite-difference version requires many additional function evaluations. The cost of these extra evaluations might or might not be significant, depending on the particular problem.
![]() | Nonlinear Equations with Analytic Jacobian | Multiobjective Examples | ![]() |