# fsolve

Solve system of nonlinear equations

## Description

Nonlinear system solver

Solves a problem specified by

F(x) = 0

for x, where F(x) is a function that returns a vector value.

x is a vector or a matrix; see Matrix Arguments.

example

x = fsolve(fun,x0) starts at x0 and tries to solve the equations fun(x) = 0, an array of zeros.

example

x = fsolve(fun,x0,options) solves the equations with the optimization options specified in options. Use optimoptions to set these options.

example

x = fsolve(problem) solves problem, a structure described in problem.

example

[x,fval] = fsolve(___), for any syntax, returns the value of the objective function fun at the solution x.

example

[x,fval,exitflag,output] = fsolve(___) additionally returns a value exitflag that describes the exit condition of fsolve, and a structure output with information about the optimization process.

[x,fval,exitflag,output,jacobian] = fsolve(___) returns the Jacobian of fun at the solution x.

## Examples

collapse all

This example shows how to solve two nonlinear equations in two variables. The equations are

Convert the equations to the form .

Write a function that computes the left-hand side of these two equations.

function F = root2d(x)

F(1) = exp(-exp(-(x(1)+x(2)))) - x(2)*(1+x(1)^2);
F(2) = x(1)*cos(x(2)) + x(2)*sin(x(1)) - 0.5;

Save this code as a file named root2d.m on your MATLAB® path.

Solve the system of equations starting at the point [0,0].

fun = @root2d;
x0 = [0,0];
x = fsolve(fun,x0)
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.

x =

0.3532    0.6061

Examine the solution process for a nonlinear system.

Set options to have no display and a plot function that displays the first-order optimality, which should converge to 0 as the algorithm iterates.

options = optimoptions('fsolve','Display','none','PlotFcn',@optimplotfirstorderopt);

The equations in the nonlinear system are

Convert the equations to the form .

Write a function that computes the left-hand side of these two equations.

function F = root2d(x)

F(1) = exp(-exp(-(x(1)+x(2)))) - x(2)*(1+x(1)^2);
F(2) = x(1)*cos(x(2)) + x(2)*sin(x(1)) - 0.5;

Save this code as a file named root2d.m on your MATLAB® path.

Solve the nonlinear system starting from the point [0,0] and observe the solution process.

fun = @root2d;
x0 = [0,0];
x = fsolve(fun,x0,options)
x =

0.3532    0.6061

Create a problem structure for fsolve and solve the problem.

Solve the same problem as in Solution with Nondefault Options, but formulate the problem using a problem structure.

Set options for the problem to have no display and a plot function that displays the first-order optimality, which should converge to 0 as the algorithm iterates.

problem.options = optimoptions('fsolve','Display','none','PlotFcn',@optimplotfirstorderopt);

The equations in the nonlinear system are

Convert the equations to the form .

Write a function that computes the left-hand side of these two equations.

function F = root2d(x)

F(1) = exp(-exp(-(x(1)+x(2)))) - x(2)*(1+x(1)^2);
F(2) = x(1)*cos(x(2)) + x(2)*sin(x(1)) - 0.5;

Save this code as a file named root2d.m on your MATLAB® path.

Create the remaining fields in the problem structure.

problem.objective = @root2d;
problem.x0 = [0,0];
problem.solver = 'fsolve';

Solve the problem.

x = fsolve(problem)
x =

0.3532    0.6061

This example returns the iterative display showing the solution process for the system of two equations and two unknowns

$\begin{array}{c}2{x}_{1}-{x}_{2}={e}^{-{x}_{1}}\\ -{x}_{1}+2{x}_{2}={e}^{-{x}_{2}}.\end{array}$

Rewrite the equations in the form :

$\begin{array}{c}2{x}_{1}-{x}_{2}-{e}^{-{x}_{1}}=0\\ -{x}_{1}+2{x}_{2}-{e}^{-{x}_{2}}=0.\end{array}$

Start your search for a solution at x0 = [-5 -5].

First, write a function that computes F, the values of the equations at x.

F = @(x) [2*x(1) - x(2) - exp(-x(1));
-x(1) + 2*x(2) - exp(-x(2))];

Create the initial point x0.

x0 = [-5;-5];

Set options to return iterative display.

options = optimoptions('fsolve','Display','iter');

Solve the equations.

[x,fval] = fsolve(F,x0,options)
Norm of      First-order   Trust-region
Iteration  Func-count     f(x)          step         optimality    radius
0          3         47071.2                      2.29e+04               1
1          6         12003.4              1       5.75e+03               1
2          9         3147.02              1       1.47e+03               1
3         12         854.452              1            388               1
4         15         239.527              1            107               1
5         18         67.0412              1           30.8               1
6         21         16.7042              1           9.05               1
7         24         2.42788              1           2.26               1
8         27        0.032658       0.759511          0.206             2.5
9         30     7.03149e-06       0.111927        0.00294             2.5
10         33     3.29525e-13     0.00169132       6.36e-07             2.5

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.
x = 2×1

0.5671
0.5671

fval = 2×1
10-6 ×

-0.4059
-0.4059

The iterative display shows f(x), which is the square of the norm of the function F(x). This value decreases to near zero as the iterations proceed. The first-order optimality measure likewise decreases to near zero as the iterations proceed. These entries show the convergence of the iterations to a solution. For the meanings of the other entries, see Iterative Display.

The fval output gives the function value F(x), which should be zero at a solution (to within the FunctionTolerance tolerance).

Find a matrix $X$ that satisfies

$X*X*X=\left[\begin{array}{cc}1& 2\\ 3& 4\end{array}\right]$,

starting at the point x0 = [1,1;1,1]. Create an anonymous function that calculates the matrix equation and create the point x0.

fun = @(x)x*x*x - [1,2;3,4];
x0 = ones(2);

Set options to have no display.

options = optimoptions('fsolve','Display','off');

Examine the fsolve outputs to see the solution quality and process.

[x,fval,exitflag,output] = fsolve(fun,x0,options)
x = 2×2

-0.1291    0.8602
1.2903    1.1612

fval = 2×2
10-9 ×

-0.1618    0.0778
0.1160   -0.0474

exitflag = 1
output = struct with fields:
iterations: 6
funcCount: 35
algorithm: 'trust-region-dogleg'
firstorderopt: 2.4095e-10
message: '...'

The exit flag value 1 indicates that the solution is reliable. To verify this manually, calculate the residual (sum of squares of fval) to see how close it is to zero.

sum(sum(fval.*fval))
ans = 4.7957e-20

This small residual confirms that x is a solution.

You can see in the output structure how many iterations and function evaluations fsolve performed to find the solution.

## Input Arguments

collapse all

Nonlinear equations to solve, specified as a function handle or function name. fun is a function that accepts a vector x and returns a vector F, the nonlinear equations evaluated at x. The equations to solve are F = 0 for all components of F. The function fun can be specified as a function handle for a file

x = fsolve(@myfun,x0)

where myfun is a MATLAB® function such as

function F = myfun(x)
F = ...            % Compute function values at x

fun can also be a function handle for an anonymous function.

x = fsolve(@(x)sin(x.*x),x0);

If the user-defined values for x and F are arrays, they are converted to vectors using linear indexing (see Array Indexing).

If the Jacobian can also be computed and the 'SpecifyObjectiveGradient' option is true, set by

the function fun must return, in a second output argument, the Jacobian value J, a matrix, at x.

If fun returns a vector (matrix) of m components and x has length n, where n is the length of x0, the Jacobian J is an m-by-n matrix where J(i,j) is the partial derivative of F(i) with respect to x(j). (The Jacobian J is the transpose of the gradient of F.)

Example: fun = @(x)x*x*x-[1,2;3,4]

Data Types: char | function_handle | string

Initial point, specified as a real vector or real array. fsolve uses the number of elements in and size of x0 to determine the number and size of variables that fun accepts.

Example: x0 = [1,2,3,4]

Data Types: double

Optimization options, specified as the output of optimoptions or a structure such as optimset returns.

Some options apply to all algorithms, and others are relevant for particular algorithms. See Optimization Options Reference for detailed information.

Some options are absent from the optimoptions display. These options appear in italics in the following table. For details, see View Options.

Example: options = optimoptions('fsolve','FiniteDifferenceType','central')

Problem structure, specified as a structure with the following fields:

Field NameEntry

objective

Objective function

x0

Initial point for x

solver

'fsolve'

options

Options created with optimoptions

Data Types: struct

## Output Arguments

collapse all

Solution, returned as a real vector or real array. The size of x is the same as the size of x0. Typically, x is a local solution to the problem when exitflag is positive. For information on the quality of the solution, see When the Solver Succeeds.

Objective function value at the solution, returned as a real vector. Generally, fval = fun(x).

Reason fsolve stopped, returned as an integer.

 1 Equation solved. First-order optimality is small. 2 Equation solved. Change in x smaller than the specified tolerance, or Jacobian at x is undefined. 3 Equation solved. Change in residual smaller than the specified tolerance. 4 Equation solved. Magnitude of search direction smaller than specified tolerance. 0 Number of iterations exceeded options.MaxIterations or number of function evaluations exceeded options.MaxFunctionEvaluations. -1 Output function or plot function stopped the algorithm. -2 Equation not solved. The exit message can have more information. -3 Equation not solved. Trust region radius became too small (trust-region-dogleg algorithm).

Information about the optimization process, returned as a structure with fields:

 iterations Number of iterations taken funcCount Number of function evaluations algorithm Optimization algorithm used cgiterations Total number of PCG iterations ('trust-region' algorithm only) stepsize Final displacement in x (not in 'trust-region-dogleg') firstorderopt Measure of first-order optimality message Exit message

Jacobian at the solution, returned as a real matrix. jacobian(i,j) is the partial derivative of fun(i) with respect to x(j) at the solution x.

## Limitations

• The function to be solved must be continuous.

• When successful, fsolve only gives one root.

• The default trust-region dogleg method can only be used when the system of equations is square, i.e., the number of equations equals the number of unknowns. For the Levenberg-Marquardt method, the system of equations need not be square.

## Tips

• For large problems, meaning those with thousands of variables or more, save memory (and possibly save time) by setting the Algorithm option to 'trust-region' and the SubproblemAlgorithm option to 'cg'.

## Algorithms

The Levenberg-Marquardt and trust-region methods are based on the nonlinear least-squares algorithms also used in lsqnonlin. Use one of these methods if the system may not have a zero. The algorithm still returns a point where the residual is small. However, if the Jacobian of the system is singular, the algorithm might converge to a point that is not a solution of the system of equations (see Limitations).

• By default fsolve chooses the trust-region dogleg algorithm. The algorithm is a variant of the Powell dogleg method described in [8]. It is similar in nature to the algorithm implemented in [7]. See Trust-Region-Dogleg Algorithm.

• The trust-region algorithm is a subspace trust-region method and is based on the interior-reflective Newton method described in [1] and [2]. Each iteration involves the approximate solution of a large linear system using the method of preconditioned conjugate gradients (PCG). See Trust-Region Algorithm.

• The Levenberg-Marquardt method is described in references [4], [5], and [6]. See Levenberg-Marquardt Method.

## Alternative Functionality

### App

The Optimize Live Editor task provides a visual interface for fsolve.

## References

[1] Coleman, T.F. and Y. Li, “An Interior, Trust Region Approach for Nonlinear Minimization Subject to Bounds,” SIAM Journal on Optimization, Vol. 6, pp. 418-445, 1996.

[2] Coleman, T.F. and Y. Li, “On the Convergence of Reflective Newton Methods for Large-Scale Nonlinear Minimization Subject to Bounds,” Mathematical Programming, Vol. 67, Number 2, pp. 189-224, 1994.

[3] Dennis, J. E. Jr., “Nonlinear Least-Squares,” State of the Art in Numerical Analysis, ed. D. Jacobs, Academic Press, pp. 269-312.

[4] Levenberg, K., “A Method for the Solution of Certain Problems in Least-Squares,” Quarterly Applied Mathematics 2, pp. 164-168, 1944.

[5] Marquardt, D., “An Algorithm for Least-squares Estimation of Nonlinear Parameters,” SIAM Journal Applied Mathematics, Vol. 11, pp. 431-441, 1963.

[6] Moré, J. J., “The Levenberg-Marquardt Algorithm: Implementation and Theory,” Numerical Analysis, ed. G. A. Watson, Lecture Notes in Mathematics 630, Springer Verlag, pp. 105-116, 1977.

[7] Moré, J. J., B. S. Garbow, and K. E. Hillstrom, User Guide for MINPACK 1, Argonne National Laboratory, Rept. ANL-80-74, 1980.

[8] Powell, M. J. D., “A Fortran Subroutine for Solving Systems of Nonlinear Algebraic Equations,” Numerical Methods for Nonlinear Algebraic Equations, P. Rabinowitz, ed., Ch.7, 1970.

## Extended Capabilities

Introduced before R2006a