CODE:
function [x,FVAL,EXITFLAG,OUTPUT,JACOB] = fsolve(FUN,x,options,varargin)
%FSOLVE solves systems of nonlinear equations of several variables.
%
% FSOLVE attempts to solve equations of the form:
%
% F(X) = 0 where F and X may be vectors or matrices.
%
% FSOLVE implements three different algorithms: trust region dogleg,
% trust region reflective, and Levenberg-Marquardt. Choose one via the
% option Algorithm: for instance, to choose trust region reflective, set
% OPTIONS = optimset('Algorithm','trust-region-reflective'), and then
% pass OPTIONS to FSOLVE.
%
% X = FSOLVE(FUN,X0) starts at the matrix X0 and tries to solve the
% equations in FUN. FUN accepts input X and returns a vector (matrix) of
% equation values F evaluated at X.
%
% X = FSOLVE(FUN,X0,OPTIONS) solves the equations with the default
% optimization parameters replaced by values in the structure OPTIONS, an
% argument created with the OPTIMSET function. See OPTIMSET for details.
% Use the Jacobian option to specify that FUN also returns a second output
% argument J that is the Jacobian matrix at the point X. If FUN returns a
% vector F of m components when X has length n, then J is an m-by-n matrix
% where J(i,j) is the partial derivative of F(i) with respect to x(j).
% (Note that the Jacobian J is the transpose of the gradient of F.)
%
% X = FSOLVE(PROBLEM) solves system defined in PROBLEM. PROBLEM is a
% structure with the function FUN in PROBLEM.objective, the start point
% in PROBLEM.x0, the options structure in PROBLEM.options, and solver
% name 'fsolve' in PROBLEM.solver. Use this syntax to solve at the
% command line a problem exported from OPTIMTOOL. The structure PROBLEM
% must have all the fields.
%
% [X,FVAL] = FSOLVE(FUN,X0,...) returns the value of the equations FUN
% at X.
%
% [X,FVAL,EXITFLAG] = FSOLVE(FUN,X0,...) returns an EXITFLAG that
% describes the exit condition of FSOLVE. Possible values of EXITFLAG and
% the corresponding exit conditions are listed below. See the
% documentation for a complete description.
%
% 1 FSOLVE converged to a root.
% 2 Change in X too small.
% 3 Change in residual norm too small.
% 4 Computed search direction too small.
% 0 Too many function evaluations or iterations.
% -1 Stopped by output/plot function.
% -2 Converged to a point that is not a root.
% -3 Trust region radius too small (Trust-region-dogleg) or
% Regularization parameter too large (Levenberg-Marquardt).
% -4 Line search failed.
%
% [X,FVAL,EXITFLAG,OUTPUT] = FSOLVE(FUN,X0,...) returns a structure
% OUTPUT with the number of iterations taken in OUTPUT.iterations, the
% number of function evaluations in OUTPUT.funcCount, the algorithm used
% in OUTPUT.algorithm, the number of CG iterations (if used) in
% OUTPUT.cgiterations, the first-order optimality (if used) in
% OUTPUT.firstorderopt, and the exit message in OUTPUT.message.
%
% [X,FVAL,EXITFLAG,OUTPUT,JACOB] = FSOLVE(FUN,X0,...) returns the
% Jacobian of FUN at X.
%
% Examples
% FUN can be specified using @:
% x = fsolve(@myfun,[2 3 4],optimset('Display','iter'))
%
% where myfun is a MATLAB function such as:
%
% function F = myfun(x)
% F = sin(x);
%
% FUN can also be an anonymous function:
%
% x = fsolve(@(x) sin(3*x),[1 4],optimset('Display','off'))
%
% If FUN is parameterized, you can use anonymous functions to cap