Solve a system of nonlinear equations defined by the function fcn. fcn should accepts a vector (array) defining the unknown variables, and return a vector of left-hand sides of the equations. Right-hand sides are defined to be zeros. In other words, this function attempts to determine a vector x such that fcn
(x)gives (approximately) all zeros. x0 determines a starting guess. The shape of x0 is preserved in all calls to fcn, but otherwise it is treated as a column vector. options is a structure specifying additional options. Currently,fsolverecognizes these options:"FunValCheck","OutputFcn","TolX","TolFun","MaxIter","MaxFunEvals","Jacobian","Updating"and"ComplexEqn".If
"Jacobian"is"on", it specifies that fcn, called with 2 output arguments, also returns the Jacobian matrix of right-hand sides at the requested point."TolX"specifies the termination tolerance in the unknown variables, while"TolFun"is a tolerance for equations. Default is1e-7for both"TolX"and"TolFun". If"Updating"is "on", the function will attempt to use Broyden updates to update the Jacobian, in order to reduce the amount of jacobian calculations. If your user function always calculates the Jacobian (regardless of number of output arguments), this option provides no advantage and should be set to false.
"ComplexEqn"is"on",fsolvewill attempt to solve complex equations in complex variables, assuming that the equations possess a complex derivative (i.e., are holomorphic). If this is not what you want, should unpack the real and imaginary parts of the system to get a real system.For description of the other options, see
optimset.On return, fval contains the value of the function fcn evaluated at x, and info may be one of the following values:
- 1
- Converged to a solution point. Relative residual error is less than specified by TolFun.
- 2
- Last relative step size was less that TolX.
- 3
- Last relative decrease in residual was less than TolF.
- 0
- Iteration limit exceeded.
- -3
- The trust region radius became excessively small.
Note: If you only have a single nonlinear equation of one variable, using
fzerois usually a much better idea.Note about user-supplied jacobians: As an inherent property of the algorithm, jacobian is always requested for a solution vector whose residual vector is already known, and it is the last accepted successful step. Often this will be one of the last two calls, but not always. If the savings by reusing intermediate results from residual calculation in jacobian calculation are significant, the best strategy is to employ OutputFcn: After a vector is evaluated for residuals, if OutputFcn is called with that vector, then the intermediate results should be saved for future jacobian evaluation, and should be kept until a jacobian evaluation is requested or until outputfcn is called with a different vector, in which case they should be dropped in favor of this most recent vector. A short example how this can be achieved follows:
function [fvec, fjac] = user_func (x, optimvalues, state) persistent sav = [], sav0 = []; if (nargin == 1) evaluation call if (nargout == 1) sav0.x = x; # mark saved vector calculate fvec, save results to sav0. elseif (nargout == 2) calculate fjac using sav. endif else outputfcn call. if (all (x == sav0.x)) sav = sav0; endif maybe output iteration status, etc. endif endfunction .... fsolve (@user_func, x0, optimset ("OutputFcn", @user_func, ...))