## Nonlinear Equations with Analytic Jacobian

This example demonstrates the use of the default trust-region-dogleg `fsolve` algorithm (see Large-Scale vs. Medium-Scale Algorithms). It is intended for problems where

• The system of nonlinear equations is square, i.e., the number of equations equals the number of unknowns.

• There exists a solution x such that F(x) = 0.

The example uses `fsolve` to obtain the minimum of the banana (or Rosenbrock) function by deriving and then solving an equivalent system of nonlinear equations. The Rosenbrock function, which has a minimum of F(x) = 0, is a common test problem in optimization. It has a high degree of nonlinearity and converges extremely slowly if you try to use steepest descent type methods. It is given by

`$f\left(x\right)=100{\left({x}_{2}-{x}_{1}^{2}\right)}^{2}+{\left(1-{x}_{1}\right)}^{2}.$`

First generalize this function to an n-dimensional function, for any positive, even value of n:

`$f\left(x\right)=\sum _{i=1}^{n/2}100{\left({x}_{2i}-{x}_{2i-1}^{2}\right)}^{2}+{\left(1-{x}_{2i-1}\right)}^{2}.$`

This function is referred to as the generalized Rosenbrock function. It consists of n squared terms involving n unknowns.

Before you can use `fsolve` to find the values of x such that F(x) = 0, i.e., obtain the minimum of the generalized Rosenbrock function, you must rewrite the function as the following equivalent system of nonlinear equations:

`$\begin{array}{c}F\left(1\right)=1-{x}_{1}\\ F\left(2\right)=10\left({x}_{2}-{x}_{1}^{2}\right)\\ F\left(3\right)=1-{x}_{3}\\ F\left(4\right)=10\left({x}_{4}-{x}_{3}^{2}\right)\\ ⋮\\ F\left(n-1\right)=1-{x}_{n-1}\\ F\left(n\right)=10\left({x}_{n}-{x}_{n-1}^{2}\right).\end{array}$`

This system is square, and you can use `fsolve` to solve it. As the example demonstrates, this system has a unique solution given by xi = 1, i = 1,...,n.

### Step 1: Write a file bananaobj.m to compute the objective function values and the Jacobian.

```function [F,J] = bananaobj(x) % Evaluate the vector function and the Jacobian matrix for % the system of nonlinear equations derived from the general % n-dimensional Rosenbrock function. % Get the problem size n = length(x); if n == 0, error('Input vector, x, is empty.'); end if mod(n,2) ~= 0, error('Input vector, x ,must have an even number of components.'); end % Evaluate the vector function odds = 1:2:n; evens = 2:2:n; F = zeros(n,1); F(odds,1) = 1-x(odds); F(evens,1) = 10.*(x(evens)-x(odds).^2); % Evaluate the Jacobian matrix if nargout > 1 if nargout > 1 c = -ones(n/2,1); C = sparse(odds,odds,c,n,n); d = 10*ones(n/2,1); D = sparse(evens,evens,d,n,n); e = -20.*x(odds); E = sparse(evens,odds,e,n,n); J = C + D + E; end```

### Step 2: Call the solve routine for the system of equations.

```n = 64; x0(1:n,1) = -1.9; x0(2:2:n,1) = 2; options = optimoptions(@fsolve,'Display','iter','SpecifyObjectiveGradient',true); [x,F,exitflag,output,JAC] = fsolve(@bananaobj,x0,options);```

Use the starting point x(i) = –1.9 for the odd indices, and x(i) = 2 for the even indices. Set `Display` to `'iter'` to see the solver's progress. Set `SpecifyObjectiveGradient` to `true` to use the Jacobian defined in `bananaobj.m`. The `fsolve` function generates the following output:

``` Norm of First-order Trust-region Iteration Func-count f(x) step optimality radius 0 1 8563.84 615 1 1 2 3093.71 1 329 1 2 3 225.104 2.5 34.8 2.5 3 4 212.48 6.25 34.1 6.25 4 5 212.48 6.25 34.1 6.25 5 6 212.48 1.5625 34.1 1.56 6 7 116.793 0.390625 5.79 0.391 7 8 109.947 0.390625 0.753 0.391 8 9 99.4696 0.976562 1.2 0.977 9 10 83.6416 2.44141 7.13 2.44 10 11 77.7663 2.44141 9.94 2.44 11 12 77.7663 2.44141 9.94 2.44 12 13 43.013 0.610352 1.38 0.61 13 14 36.4334 0.610352 1.58 0.61 14 15 34.1448 1.52588 6.71 1.53 15 16 18.0108 1.52588 4.91 1.53 16 17 8.48336 1.52588 3.74 1.53 17 18 3.74566 1.52588 3.58 1.53 18 19 1.46166 1.52588 3.32 1.53 19 20 0.29997 1.24265 1.94 1.53 20 21 0 0.0547695 0 1.53 Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.```