- publishing free software manuals
 GNU Octave Manual Version 3 by John W. Eaton, David Bateman, Søren HaubergPaperback (6"x9"), 568 pagesISBN 095461206XRRP £24.95 (\$39.95)

# 19 Nonlinear Equations

Octave can solve sets of nonlinear equations of the form

```F (x) = 0
```

using the function `fsolve`, which is based on the Minpack subroutine `hybrd`. This is an iterative technique so a starting point must be provided, and convergence is not guaranteed even if a solution exists.

Loadable Function: [x, fval, info] = fsolve (fcn, x0)
Given fcn, the name of a function of the form `f (x)` and an initial starting point x0, `fsolve` solves the set of equations such that `f(x) == 0`.

On return, fval contains the value of the function fcn evaluated at x, and info may be one of the following values:

-2
Invalid input parameters.
-1
Error in user-supplied function.
1
Relative error between two consecutive iterates is at most the specified tolerance (see `fsolve_options`).
3
Algorithm failed to converge.
4
Limit on number of function calls reached.

If fcn is a two-element string array, or a two element cell array containing either the function name or inline or function handle. The first element names the function f described above, and the second element names a function of the form `j (x)` to compute the Jacobian matrix with elements

```           df_i
jac(i,j) = ----
dx_j
```

You can use the function `fsolve_options` to set optional parameters for `fsolve`.

When called with two arguments, this function allows you to set options parameters for the function `fsolve`. Given one argument, `fsolve_options` returns the value of the corresponding option. If no arguments are supplied, the names of all the available options and their current values are displayed.

Options include

`"tolerance"`
Nonnegative relative tolerance.

Here is a complete example. To solve the set of equations

```-2x^2 + 3xy   + 4 sin(y) = 6
3x^2 - 2xy^2 + 3 cos(x) = -4
```

you first need to write a function to compute the value of the given function. For example:

```function y = f (x)
y(1) = -2*x(1)^2 + 3*x(1)*x(2)   + 4*sin(x(2)) - 6;
y(2) =  3*x(1)^2 - 2*x(1)*x(2)^2 + 3*cos(x(1)) + 4;
endfunction
```

Then, call `fsolve` with a specified initial condition to find the roots of the system of equations. For example, given the function `f` defined above,

```[x, fval, info] = fsolve (@f, [1; 2])
```

results in the solution

```x =

0.57983
2.54621

fval =

-5.7184e-10
5.5460e-10

info = 1
```

A value of `info = 1` indicates that the solution has converged.

The function `perror` may be used to print English messages corresponding to the numeric error codes. For example,

```perror ("fsolve", 1)
-| solution converged to requested tolerance
```

When no Jacobian is supplied (as in the example above) it is approximated numerically. This requires more function evaluations, and hence is less efficient. In the example above we could derive the Jacobian analytically as

```function J = jacobian(x)
J(1,1) =  3*x(2) - 4*x(1);
J(1,2) =  4*cos(x(2)) + 3*x(1);
J(2,1) = -2*x(2)^2 - 3*sin(x(1)) + 6*x(1);
J(2,2) = -4*x(1)*x(2);
endfunction
```

The Jacobian can then be used with the following call to `fsolve`:

```[x, fval, info] = fsolve ({@f, @jacobian}, [1; 2]);
```

which gives the same solution as before.

 ISBN 095461206X GNU Octave Manual Version 3 See the print edition