Documentation |
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(x)=100{\left({x}_{2}-{x}_{1}^{2}\right)}^{2}+{(1-{x}_{1})}^{2}.$$
First generalize this function to an n-dimensional function, for any positive, even value of n:
$$f(x)={\displaystyle \sum _{i=1}^{n/2}100{\left({x}_{2i}-{x}_{2i-1}^{2}\right)}^{2}+{(1-{x}_{2i-1})}^{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(1)=1-{x}_{1}\\ F(2)=10\left({x}_{2}-{x}_{1}^{2}\right)\\ F(3)=1-{x}_{3}\\ F(4)=10\left({x}_{4}-{x}_{3}^{2}\right)\\ \vdots \\ F(n-1)=1-{x}_{n-1}\\ F(n)=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 x_{i} = 1, i = 1,...,n.
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
n = 64; x0(1:n,1) = -1.9; x0(2:2:n,1) = 2; options = optimoptions(@fsolve,'Display','iter','Jacobian','on'); [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 Jacobian to 'on' 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 102.771 1.5625 6.39 1.56 6 7 102.771 3.90625 6.39 3.91 7 8 87.7443 0.976563 2.19 0.977 8 9 74.1426 2.44141 6.27 2.44 9 10 74.1426 2.44141 6.27 2.44 10 11 52.497 0.610352 1.52 0.61 11 12 41.3297 1.52588 4.63 1.53 12 13 34.5115 1.52588 6.97 1.53 13 14 16.9716 1.52588 4.69 1.53 14 15 8.16797 1.52588 3.77 1.53 15 16 3.55178 1.52588 3.56 1.53 16 17 1.38476 1.52588 3.31 1.53 17 18 0.219553 1.16206 1.66 1.53 18 19 0 0.0468565 0 1.53 Equation solved. fsolve completed because the vector of function values is near zero as measured by the default value of the function tolerance, and the problem appears regular as measured by the gradient.