fsolve

Find a solution of a system of real nonlinear equations.

Syntax

x = fsolve(@func,x0)

x = fsolve(@func,x0,options)

[x,fval,info,output] = fsolve(...)

Inputs

func
The system to solve. See the optimset option Jacobian for details.
x0
An estimate of the solution.
options
A struct containing option settings.
See optimset for details.

Outputs

x
A solution of the system.
fval
The value of func evaulated at x.
info
The convergence status flag.
info = 4
Relative step size converged to within tolX.
info = 3
Relative function value converged to within tolFun.
info = 2
Step size converged to within tolX.
info = 1
Function value converged to within tolFun.
info = 0
Reached maximum number of iterations or function calls, or the algorithm aborted because it was not converging.
info = -3
Trust region became too small to continue.
output
A struct containing iteration details. The members are as follows:
iterations
The number of iterations.
nfev
The number of function evaluations.
xiter
The candidate solution at each iteration.
fvaliter
The objective function value at each iteration.

Examples

Solve the system of equations SysFunc.
function res = SysFunc(x)
    % intersection of two paraboloids and a plane
    v1 = (x(1))^2 + (x(2))^2 + 6;
    v2 = 2*(x(1))^2 + 2*(x(2))^2 + 4;
    v3 = 5*x(1) - 5*x(2);
    res = zeros(2,1);
    res(1,1) = v1 - v3;
    res(2,1) = v2 - v3;
end

x0 = [1; 2];
[x,fval] = fsolve(@SysFunc,x0)
x = [Matrix] 2 x 1
 1.40000
-0.20000
fval = [Matrix] 2 x 1
3.67339e-07
7.34677e-07
Modify the previous example to pass an extra parameter to the user function using a function handle.
function res = SysFunc(x, offset)
    % intersection of two paraboloids and a plane
    v1 = (x(1))^2 + (x(2))^2 + offset(1);
    v2 = 2*(x(1))^2 + 2*(x(2))^2 + offset(2);
    v3 = 5*x(1) - 5*x(2) + offset(3);
    res = zeros(2,1);
    res(1,1) = v1 - v3;
    res(2,1) = v2 - v3;
end

handle = @(x) SysFunc(x, [8,6,4]);
[x,fval] = fsolve(handle,x0)
x = [Matrix] 2 x 1
1.40000
0.20000
fval = [Matrix] 2 x 1
0
0

Comments

fsolve uses a modified Gauss-Netwon algorithm with a trust region method.

Options for convergence tolerance controls and analytical derivatives are specified with optimset.

If fsolve converges to a solution that is not a zero of the system, it will produce a warning indicating that a best fit value is being returned.

To pass additional parameters to a function argument, use an anonymous function.

The optimset options and defaults are as follows.
  • MaxIter: 400
  • MaxFunEvals: 1,000,000
  • TolFun: 1.0e-7
  • TolX: 1.0e-7
  • Jacobian: 'off'
  • Display: 'off'