Help - Fsolve - MATLAB
Help - Fsolve - MATLAB
fsolve
Solve system of nonlinear equations
Equation
Solves a problem specified by
F(x) = 0
for x, where x is a vector and F(x) is a function that returns a vector value.
Syntax
x = fsolve(fun,x0)
x = fsolve(fun,x0,options)
x = fsolve(problem)
[x,fval] = fsolve(fun,x0)
[x,fval,exitflag] = fsolve(...)
[x,fval,exitflag,output] = fsolve(...)
[x,fval,exitflag,output,jacobian] = fsolve(...)
Description
fsolve finds a root (zero) of a system of nonlinear equations.
Note Passing Extra Parameters explains how to pass extra parameters to the
system of equations, if necessary.
fun The nonlinear system of equations to solve. fun is a function that accepts a
vector x and returns a vector F, the nonlinear equations evaluated at x. The
function fun can be specified as a function handle for a file
x = fsolve(@myfun,x0)
function F = myfun(x)
F = ... % Compute function values at x
x = fsolve(@(x)sin(x.*x),x0);
If the user−defined values forx and F are matrices, they are converted to a
vector using linear indexing.
If the Jacobian can also be computed and the Jacobian option is 'on', set by
options = optimset('Jacobian','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 ofF(i) with respect to x(j). (The Jacobian J is the
transpose of the gradient of F.)
problem objective Objective function
x0 Initial point for x
solver 'fsolve'
options Options structure created with optimset
Output Arguments
Function Arguments contains general descriptions of arguments returned by fsolve. For
more information on the output headings for fsolve, see Function−Specific Headings.
This section provides function−specific details for exitflag and output:
exitflag Integer identifying the reason the algorithm terminated. The following lists
the values of exitflag and the corresponding reasons the algorithm
terminated.
1 Function converged to a solution x.
2 Change in x was smaller than the specified
tolerance.
3 Change in the residual was smaller than the
specified tolerance.
4 Magnitude of search direction was smaller than
the specified tolerance.
0 Number of iterations exceeded
options.MaxIter or number of function
evaluations exceeded options.FunEvals .
-1 Output function terminated the algorithm.
-2 Algorithm appears to be converging to a point that
is not a root.
-3 Trust region radius became too small
(trust-region-dogleg algorithm) or
regularization parameter became too large
(levenberg-marquardt algorithm).
-4 Line search cannot sufficiently decrease the
residual along the current search direction.
output Structure containing information about the optimization. The fields of the
structure are
iterations Number of iterations taken
funcCount Number of function evaluations
algorithm Optimization algorithm used.
cgiterations Total number of PCG iterations (large−scale
algorithm only)
stepsize Final displacement in x (Gauss−Newton and
Levenberg−Marquardt algorithms)
firstorderopt Measure of first−order optimality (dogleg or
large−scale algorithm, [ ] for others)
message Exit message
Options
Optimization options used by fsolve. Some options apply to all algorithms, some are only
relevant when using the trust−region−reflective algorithm, and others are only relevant when
using the other algorithms. You can use optimset to set or change the values of these
fields in the options structure, options. See Optimization Options for detailed information.
All Algorithms
All algorithms use the following options:
W = jmfun(Jinfo,Y,flag)
[F,Jinfo] = fun(x)
Examples
Example 1
This example solves the system of two equations and two unknowns:
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 somewhere on your MATLAB path. Next, set up the
initial point and options and call fsolve:
Equation solved.
x =
0.5671
0.5671
fval =
1.0e-006 *
-0.4059
-0.4059
Example 2
Find a matrix x that satisfies the equation
function F = myfun(x)
F = x*x*x-[1,2;3,4];
Save this function file as myfun.m somewhere on your MATLAB path. Next, set up an
initial point and options and call fsolve:
The solution is
x =
-0.1291 0.8602
1.2903 1.1612
Fval =
1.0e-009 *
-0.1619 0.0777
0.1162 -0.0466
exitflag =
1
sum(sum(Fval.*Fval))
ans =
4.7935e-020
Notes
If the system of equations is linear, use \ (matrix left division) for better speed and accuracy.
For example, to find the solution to the following linear system of equations:
3x1 + 11x2 ˘ 2x3 = 7
x1 + x2 ˘ 2x3 = 4
x1 ˘ x2 + x3 = 19.
Formulate and solve the problem as
Algorithm
The Gauss−Newton, Levenberg−Marquardt, and trust−region−reflective 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 and Diagnostics
following).
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]. It is described in the User’s Guide in Trust−Region
Dogleg Method.
The trust−region−reflective 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 Reflective fsolve
Algorithm.
The Levenberg−Marquardt method is described in[4], [5], and [6]. It is described in
the User’s Guide in Levenberg−Marquardt Method.
The medium−scale Gauss−Newton method [3] with line search is described in the
User’s Guide in Gauss−Newton Method. The default line search algorithm for the
Gauss−Newton algorithm , i.e., the LineSearchType option, is 'quadcubic' .
This is a safeguarded mixed quadratic and cubic polynomial interpolation and
extrapolation method. A safeguarded cubic polynomial method can be selected by
setting LineSearchType to 'cubicpoly' . This method generally requires fewer
function evaluations but more gradient evaluations. Thus, if gradients are being
supplied and can be calculated inexpensively, the cubic polynomial line search
method is preferable.
Diagnostics
All Algorithms
fsolve may converge to a nonzero point and give this message:
Trust−Region−Dogleg Algorithm
For the trust−region dogleg method, fsolve stops if the step size becomes too small and it
can make no more progress. fsolve gives this message:
Limitations
The function to be solved must be continuous. When successful, fsolve only gives one
root. fsolve may converge to a nonzero point, in which case, try other starting values.
fsolve only handles real variables. When x has complex variables, the variables must be
split into real and imaginary parts.
Trust−Region−Reflective Algorithm
The preconditioner computation used in the preconditioned conjugate gradient part of the
trust−region−reflective algorithm formsJ T J (where J is the Jacobian matrix) before computing
the preconditioner; therefore, a row of J with many nonzeros, which results in a nearly dense
product J T J, might lead to a costly solution process for large problems.
Large−Scale Problem Coverage and Requirements
Number of Equations
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 and Gauss−Newton methods, the system of equations need not be
square.
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.
See Also
@ (function_handle) , \ (matrix left division), lsqcurvefit , lsqnonlin ,
optimset , optimtool , Anonymous Functions
For more details about the fsolve algorithms, see Equation Solving. For more examples of
equation solving, see Equation Solving Examples.