Solve system of nonlinear equations
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.
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(...)
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. 
x = fsolve(fun,x0)
starts
at x0
and tries to solve the equations described
in fun
.
x = fsolve(fun,x0,options)
solves
the equations with the optimization options specified in options
.
Use optimoptions
to set these
options.
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.
[x,fval] = fsolve(fun,x0)
returns
the value of the objective function fun
at the
solution x
.
[x,fval,exitflag] = fsolve(...)
returns
a value exitflag
that describes the exit condition.
[x,fval,exitflag,output] = fsolve(...)
returns
a structure output
that contains information about
the optimization.
[x,fval,exitflag,output,jacobian] = fsolve(...)
returns
the Jacobian of fun
at the solution x
.
Function Arguments contains
general descriptions of arguments passed into fsolve
.
This section provides functionspecific details for fun
and problem
:
 The nonlinear system
of equations to solve. x = fsolve(@myfun,x0) where function F = myfun(x) F = ... % Compute function values at x
x = fsolve(@(x)sin(x.*x),x0); If the userdefined values for If
the Jacobian can also be computed and the Jacobian
option is options = optimoptions('fsolve','Jacobian','on') the function If  
problem 
 Objective function 
 Initial point for x  
 'fsolve'  
 Options created with optimoptions 
Function Arguments contains
general descriptions of arguments returned by fsolve
.
For more information on the output headings for fsolve
,
see FunctionSpecific Headings.
This section provides functionspecific details for exitflag
and output
:
 Integer identifying the
reason the algorithm terminated. The following lists the values of  
 Function converged to a solution  
 Change in  
 Change in the residual was smaller than the specified tolerance.  
 Magnitude of search direction was smaller than the specified tolerance.  
 Number of iterations exceeded  
 Output function terminated the algorithm.  
 Algorithm appears to be converging to a point that is not a root.  
 Trust region radius became too small (  
 Line search cannot sufficiently decrease the residual along the current search direction.  
 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 (trustregionreflective algorithm only)  
stepsize  Final displacement in  
firstorderopt  Measure of firstorder optimality  
message  Exit message 
Optimization options used by fsolve
. Some
options apply to all algorithms, some are only relevant when using
the trustregionreflective algorithm, and others are only relevant
when using the other algorithms. Use optimoptions
to
set or change options
. See Optimization Options Reference for detailed information.
All algorithms use the following options:
 Choose between The 
 Compare usersupplied derivatives
(gradients of objective or constraints) to finitedifferencing derivatives.
The choices are 
 Display diagnostic information
about the function to be minimized or solved. The choices are 
 Maximum change in variables for
finitedifference gradients (a positive scalar). The default is 
 Minimum change in variables for
finitedifference gradients (a positive scalar). The default is 
Display  Level of display:

FinDiffRelStep  Scalar or vector step size factor. When you set
and central finite differences are
Scalar 
FinDiffType  Finite differences, used to estimate gradients,
are either 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. 
FunValCheck  Check whether objective function
values are valid. 
 If 
 Maximum number of function evaluations
allowed, a positive integer. The default is 
 Maximum number of iterations allowed,
a positive integer. The default is 
OutputFcn  Specify one or more userdefined
functions that an optimization function calls at each iteration, either
as a function handle or as a cell array of function handles. The default
is none ( 
 Plots various measures of progress
while the algorithm executes. Select from predefined plots or write
your own. Pass a function handle or a cell array of function handles.
The default is none (
For information on writing a custom plot function, see Plot Functions. 
 Termination tolerance on the function
value, a positive scalar. The default is 
 Termination tolerance on 
 Typical The 
The trustregionreflective algorithm uses the following options:
 Function handle for
Jacobian multiply function. For largescale structured problems, this
function computes the Jacobian matrix product W = jmfun(Jinfo,Y,flag) where [F,Jinfo] = fun(x)
In each case,
See Minimization with Dense Structured Hessian, Linear Equalities for a similar example.  
 Sparsity pattern of the Jacobian
for finite differencing. Set Use In
the worst case, if the structure is unknown, do not set  
 Maximum number of PCG (preconditioned
conjugate gradient) iterations, a positive scalar. The default is  
 Upper bandwidth of preconditioner
for PCG, a nonnegative integer. The default  
 Termination tolerance on the PCG
iteration, a positive scalar. The default is 
The LevenbergMarquardt algorithm uses the following option:
 Initial value of the LevenbergMarquardt parameter, a
positive scalar. Default is 


This example solves 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
somewhere
on your MATLAB path. Next, set up the initial point and options
and call fsolve
:
x0 = [5; 5]; % Make a starting guess at the solution options = optimoptions('fsolve','Display','iter'); % Option to display output [x,fval] = fsolve(@myfun,x0,options) % Call solver
After several iterations, fsolve
finds
an answer:
Norm of Firstorder Trustregion Iteration Funccount f(x) step optimality radius 0 3 23535.6 2.29e+004 1 1 6 6001.72 1 5.75e+003 1 2 9 1573.51 1 1.47e+003 1 3 12 427.226 1 388 1 4 15 119.763 1 107 1 5 18 33.5206 1 30.8 1 6 21 8.35208 1 9.05 1 7 24 1.21394 1 2.26 1 8 27 0.016329 0.759511 0.206 2.5 9 30 3.51575e006 0.111927 0.00294 2.5 10 33 1.64763e013 0.00169132 6.36e007 2.5 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. x = 0.5671 0.5671 fval = 1.0e006 * 0.4059 0.4059
Find a matrix x
that satisfies the equation
$$X*X*X=\left[\begin{array}{cc}1& 2\\ 3& 4\end{array}\right],$$
starting at the point x= [1,1; 1,1]
.
First, write a file that computes the equations to be solved.
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
:
x0 = ones(2,2); % Make a starting guess at the solution options = optimoptions('fsolve','Display','off'); % Turn off display [x,Fval,exitflag] = fsolve(@myfun,x0,options)
The solution is
x = 0.1291 0.8602 1.2903 1.1612 Fval = 1.0e009 * 0.1621 0.0780 0.1167 0.0465 exitflag = 1
and the residual is close to zero.
sum(sum(Fval.*Fval)) ans = 4.8133e20
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:
3x_{1} + 11x_{2} –
2x_{3} = 7
x_{1} + x_{2} –
2x_{3} = 4
x_{1} – x_{2} + x_{3} =
19.
Formulate and solve the problem as
A = [ 3 11 2; 1 1 2; 1 1 1]; b = [ 7; 4; 19]; x = A\b x = 13.2188 2.3438 3.4375
You can use the trustregion reflective algorithm in lsqnonlin
, lsqcurvefit
,
and fsolve
with small to mediumscale
problems without computing the Jacobian in fun
or
providing the Jacobian sparsity pattern. (This also applies to using fmincon
or fminunc
without
computing the Hessian or supplying the Hessian sparsity pattern.)
How small is small to mediumscale? No absolute answer is available,
as it depends on the amount of virtual memory in your computer system
configuration.
Suppose your problem has m
equations and n
unknowns.
If the command J = sparse(ones(m,n))
causes
an Out of memory
error on your machine,
then this is certainly too large a problem. If it does not result
in an error, the problem might still be too large. You can only find
out by running it and seeing if MATLAB runs within the amount
of virtual memory available on your system.
fsolve
may converge to a nonzero point and
give this message:
Optimizer is stuck at a minimum that is not a root Try again with a new starting guess
In this case, run fsolve
again with other
starting values.
For the trustregion dogleg method, fsolve
stops
if the step size becomes too small and it can make no more progress. fsolve
gives
this message:
The optimization algorithm can make no further progress: Trust region radius less than 10*eps
In this case, run fsolve
again with other
starting values.
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.
The preconditioner computation used in the preconditioned conjugate gradient part of the trustregionreflective algorithm forms J^{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.
TrustRegionReflective Problem Coverage and Requirements
For Large Problems 


The default trustregion 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 LevenbergMarquardt method, the system of equations need not be square.
[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. 418445, 1996.
[2] Coleman, T.F. and Y. Li, "On the Convergence of Reflective Newton Methods for LargeScale Nonlinear Minimization Subject to Bounds," Mathematical Programming, Vol. 67, Number 2, pp. 189224, 1994.
[3] Dennis, J. E. Jr., "Nonlinear LeastSquares," State of the Art in Numerical Analysis, ed. D. Jacobs, Academic Press, pp. 269312.
[4] Levenberg, K., "A Method for the Solution of Certain Problems in LeastSquares," Quarterly Applied Mathematics 2, pp. 164168, 1944.
[5] Marquardt, D., "An Algorithm for Leastsquares Estimation of Nonlinear Parameters," SIAM Journal Applied Mathematics, Vol. 11, pp. 431441, 1963.
[6] Moré, J. J., "The LevenbergMarquardt Algorithm: Implementation and Theory," Numerical Analysis, ed. G. A. Watson, Lecture Notes in Mathematics 630, Springer Verlag, pp. 105116, 1977.
[7] Moré, J. J., B. S. Garbow, and K. E. Hillstrom, User Guide for MINPACK 1, Argonne National Laboratory, Rept. ANL8074, 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.
\
 lsqcurvefit
 lsqnonlin
 optimoptions
 optimtool