Documentation

# fsolve

Solve system of nonlinear equations

## Syntax

``x = fsolve(fun,x0)``
``x = fsolve(fun,x0,options)``
``x = fsolve(problem)``
``````[x,fval] = fsolve(___)``````
``````[x,fval,exitflag,output] = fsolve(___)``````
``````[x,fval,exitflag,output,jacobian] = fsolve(___)``````

## 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`, where `problem` is a structure described in Input Arguments. Create the `problem` structure by exporting a problem from Optimization app, as described in Exporting Your Work.```

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 F(x) = 0:

`$\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 file that computes `F`, the values of the equations at `x`.

```function F = myfun(x) F = [2*x(1) - x(2) - exp(-x(1)); -x(1) + 2*x(2) - exp(-x(2))];```

Save this function file as `myfun.m` on your MATLAB® path.

Set up the initial point. Set options to return iterative display.

```x0 = [-5;-5]; options = optimoptions('fsolve','Display','iter');```

Call `fsolve`.

`[x,fval] = fsolve(@myfun,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 = 0.5671 0.5671 fval = 1.0e-006 * -0.4059 -0.4059```

Find a matrix `X` that satisfies

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

starting at the point `x= [1,1;1,1]`. Examine the `fsolve` outputs to see the solution quality and process.

Create an anonymous function that calculates the matrix equation.

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

Set options to turn off the display. Set the initial point `x0`.

```options = optimoptions('fsolve','Display','off'); x0 = ones(2);```

Call `fsolve` and obtain information about the solution process.

```[x,fval,exitflag,output] = fsolve(fun,x0,options); x,fval,exitflag```
```x = -0.1291 0.8602 1.2903 1.1612 fval = 1.0e-09 * -0.1618 0.0778 0.1160 -0.0474 exitflag = 1```

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.

`fsolve` performed 35 function evaluations to find the solution, as you can see in the `output` structure.

`output.funcCount`
```ans = 35```

## 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 (MATLAB)).

If the Jacobian can also be computed and the Jacobian option is `'on'`, set by

`options = optimoptions('fsolve','SpecifyObjectiveGradient','on')`

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.

All Algorithms
`Algorithm`

Choose between `'trust-region-dogleg'` (default), `'trust-region'`, and `'levenberg-marquardt'`.

The `Algorithm` option specifies a preference for which algorithm to use. It is only a preference because for the trust-region algorithm, the nonlinear system of equations cannot be underdetermined; that is, the number of equations (the number of elements of `F` returned by `fun`) must be at least as many as the length of `x`. Similarly, for the trust-region-dogleg algorithm, the number of equations must be the same as the length of `x`. `fsolve` uses the Levenberg-Marquardt algorithm when the selected algorithm is unavailable. For more information on choosing the algorithm, see Choosing the Algorithm.

To set some algorithm options using `optimset` instead of `optimoptions`:

• `Algorithm` — Set the algorithm to `'trust-region-reflective'` instead of `'trust-region'`.

• InitDamping — Set the initial Levenberg-Marquardt parameter λ by setting `Algorithm` to a cell array such as `{'levenberg-marquardt',.005}`.

`CheckGradients`

Compare user-supplied derivatives (gradients of objective or constraints) to finite-differencing derivatives. The choices are `true` or the default `false`.

For `optimset`, the name is `DerivativeCheck` and the values are `'on'` or `'off'`. See Current and Legacy Option Name Tables.

Diagnostics

Display diagnostic information about the function to be minimized or solved. The choices are `'on'` or the default `'off'`.

DiffMaxChange

Maximum change in variables for finite-difference gradients (a positive scalar). The default is `Inf`.

DiffMinChange

Minimum change in variables for finite-difference gradients (a positive scalar). The default is `0`.

`Display`

Level of display (see Iterative Display):

• `'off'` or `'none'` displays no output.

• `'iter'` displays output at each iteration, and gives the default exit message.

• `'iter-detailed'` displays output at each iteration, and gives the technical exit message.

• `'final'` (default) displays just the final output, and gives the default exit message.

• `'final-detailed'` displays just the final output, and gives the technical exit message.

`FiniteDifferenceStepSize`

Scalar or vector step size factor for finite differences. When you set `FiniteDifferenceStepSize` to a vector `v`, the forward finite differences `delta` are

`delta = v.*sign′(x).*max(abs(x),TypicalX);`

where `sign′(x) = sign(x)` except `sign′(0) = 1`. Central finite differences are

`delta = v.*max(abs(x),TypicalX);`

Scalar `FiniteDifferenceStepSize` expands to a vector. The default is `sqrt(eps)` for forward finite differences, and `eps^(1/3)` for central finite differences.

For `optimset`, the name is `FinDiffRelStep`. See Current and Legacy Option Name Tables.

`FiniteDifferenceType`

Finite differences, used to estimate gradients, are either `'forward'` (default), or `'central'` (centered). `'central'` takes twice as many function evaluations, but should be more accurate.

The algorithm is careful to obey bounds when estimating both types of finite differences. So, for example, it could take a backward, rather than a forward, difference to avoid evaluating at a point outside bounds.

For `optimset`, the name is `FinDiffType`. See Current and Legacy Option Name Tables.

`FunctionTolerance`

Termination tolerance on the function value, a positive scalar. The default is `1e-6`. See Tolerances and Stopping Criteria.

For `optimset`, the name is `TolFun`. See Current and Legacy Option Name Tables.

FunValCheck

Check whether objective function values are valid. `'on'` displays an error when the objective function returns a value that is `complex`, `Inf`, or `NaN`. The default, `'off'`, displays no error.

`MaxFunctionEvaluations`

Maximum number of function evaluations allowed, a positive integer. The default is `100*numberOfVariables`. See Tolerances and Stopping Criteria and Iterations and Function Counts.

For `optimset`, the name is `MaxFunEvals`. See Current and Legacy Option Name Tables.

`MaxIterations`

Maximum number of iterations allowed, a positive integer. The default is `400`. See Tolerances and Stopping Criteria and Iterations and Function Counts.

For `optimset`, the name is `MaxIter`. See Current and Legacy Option Name Tables.

`OptimalityTolerance`

Termination tolerance on the first-order optimality (a positive scalar). The default is `1e-6`. See First-Order Optimality Measure.

Internally, the `'levenberg-marquardt'` algorithm uses an optimality tolerance (stopping criterion) of `1e-4` times `FunctionTolerance` and does not use `OptimalityTolerance`.

`OutputFcn`

Specify one or more user-defined functions that an optimization function calls at each iteration. Pass a function handle or a cell array of function handles. The default is none (`[]`). See Output Function Syntax.

`PlotFcn`

Plots various measures of progress while the algorithm executes; select from predefined plots or write your own. Pass a built-in plot function name, a function handle, or a cell array of built-in plot function names or function handles. For custom plot functions, pass function handles. The default is none (`[]`):

• `'optimplotx'` plots the current point.

• `'optimplotfunccount'` plots the function count.

• `'optimplotfval'` plots the function value.

• `'optimplotstepsize'` plots the step size.

• `'optimplotfirstorderopt'` plots the first-order optimality measure.

For information on writing a custom plot function, see Plot Function Syntax.

For `optimset`, the name is `PlotFcns`. See Current and Legacy Option Name Tables.

`SpecifyObjectiveGradient`

If `true`, `fsolve` uses a user-defined Jacobian (defined in `fun`), or Jacobian information (when using `JacobianMultiplyFcn`), for the objective function. If `false` (default), `fsolve` approximates the Jacobian using finite differences.

For `optimset`, the name is `Jacobian` and the values are `'on'` or `'off'`. See Current and Legacy Option Name Tables.

`StepTolerance`

Termination tolerance on `x`, a positive scalar. The default is `1e-6`. See Tolerances and Stopping Criteria.

For `optimset`, the name is `TolX`. See Current and Legacy Option Name Tables.

`TypicalX`

Typical `x` values. The number of elements in `TypicalX` is equal to the number of elements in `x0`, the starting point. The default value is `ones(numberofvariables,1)`. `fsolve` uses `TypicalX` for scaling finite differences for gradient estimation.

The `trust-region-dogleg` algorithm uses `TypicalX` as the diagonal terms of a scaling matrix.

`UseParallel`

When `true`, `fsolve` estimates gradients in parallel. Disable by setting to the default, `false`. See Parallel Computing.

trust-region Algorithm
`JacobianMultiplyFcn`

Jacobian multiply function, specified as a function handle. For large-scale structured problems, this function computes the Jacobian matrix product `J*Y`, `J'*Y`, or `J'*(J*Y)` without actually forming `J`. The function is of the form

`W = jmfun(Jinfo,Y,flag)`

where `Jinfo` contains a matrix used to compute `J*Y` (or `J'*Y`, or `J'*(J*Y)`). The first argument `Jinfo` must be the same as the second argument returned by the objective function `fun`, for example, in

`[F,Jinfo] = fun(x)`

`Y` is a matrix that has the same number of rows as there are dimensions in the problem. `flag` determines which product to compute:

• If `flag == 0`, `W = J'*(J*Y)`.

• If `flag > 0`, ```W = J*Y```.

• If `flag < 0`, ```W = J'*Y```.

In each case, `J` is not formed explicitly. `fsolve` uses `Jinfo` to compute the preconditioner. See Passing Extra Parameters for information on how to supply values for any additional parameters `jmfun` needs.

### Note

`'SpecifyObjectiveGradient'` must be set to `true` for `fsolve` to pass `Jinfo` from `fun` to `jmfun`.

See Minimization with Dense Structured Hessian, Linear Equalities for a similar example.

For `optimset`, the name is `JacobMult`. See Current and Legacy Option Name Tables.

JacobPattern

Sparsity pattern of the Jacobian for finite differencing. Set `JacobPattern(i,j) = 1` when `fun(i)` depends on `x(j)`. Otherwise, set ```JacobPattern(i,j) = 0```. In other words, `JacobPattern(i,j) = 1` when you can have ∂`fun(i)`/∂`x(j)` ≠ 0.

Use `JacobPattern` when it is inconvenient to compute the Jacobian matrix `J` in `fun`, though you can determine (say, by inspection) when `fun(i)` depends on `x(j)`. `fsolve` can approximate `J` via sparse finite differences when you give `JacobPattern`.

In the worst case, if the structure is unknown, do not set `JacobPattern`. The default behavior is as if `JacobPattern` is a dense matrix of ones. Then `fsolve` computes a full finite-difference approximation in each iteration. This can be very expensive for large problems, so it is usually better to determine the sparsity structure.

MaxPCGIter

Maximum number of PCG (preconditioned conjugate gradient) iterations, a positive scalar. The default is `max(1,floor(numberOfVariables/2))`. For more information, see Equation Solving Algorithms.

PrecondBandWidth

Upper bandwidth of preconditioner for PCG, a nonnegative integer. The default `PrecondBandWidth` is `Inf`, which means a direct factorization (Cholesky) is used rather than the conjugate gradients (CG). The direct factorization is computationally more expensive than CG, but produces a better quality step towards the solution. Set `PrecondBandWidth` to `0` for diagonal preconditioning (upper bandwidth of 0). For some problems, an intermediate bandwidth reduces the number of PCG iterations.

`SubproblemAlgorithm`

Determines how the iteration step is calculated. The default, `'factorization'`, takes a slower but more accurate step than `'cg'`. See Trust-Region Algorithm.

TolPCG

Termination tolerance on the PCG iteration, a positive scalar. The default is `0.1`.

Levenberg-Marquardt Algorithm
InitDamping

Initial value of the Levenberg-Marquardt parameter, a positive scalar. Default is `1e-2`. For details, see Levenberg-Marquardt Method.

ScaleProblem

`'jacobian'` can sometimes improve the convergence of a poorly scaled problem. The default is `'none'`.

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`

The simplest way of obtaining a `problem` structure is to export the problem from the Optimization app.

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. `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 . It is similar in nature to the algorithm implemented in . 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  and . 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 , , and . See Levenberg-Marquardt Method.

 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.

 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.

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

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

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

 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.

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

 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.

Watch now