Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

# NAG Toolbox: nag_roots_sys_func_expert (c05qc)

## Purpose

nag_roots_sys_func_expert (c05qc) is a comprehensive function that finds a solution of a system of nonlinear equations by a modification of the Powell hybrid method.

## Syntax

[x, fvec, diag, nfev, fjac, r, qtf, user, ifail] = c05qc(fcn, x, ml, mu, mode, diag, nprint, 'n', n, 'xtol', xtol, 'maxfev', maxfev, 'epsfcn', epsfcn, 'factor', factor, 'user', user)
[x, fvec, diag, nfev, fjac, r, qtf, user, ifail] = nag_roots_sys_func_expert(fcn, x, ml, mu, mode, diag, nprint, 'n', n, 'xtol', xtol, 'maxfev', maxfev, 'epsfcn', epsfcn, 'factor', factor, 'user', user)

## Description

The system of equations is defined as:
 $fi x1,x2,…,xn = 0 , ​ i= 1, 2, …, n .$
nag_roots_sys_func_expert (c05qc) is based on the MINPACK routine HYBRD (see Moré et al. (1980)). It chooses the correction at each step as a convex combination of the Newton and scaled gradient directions. The Jacobian is updated by the rank-1 method of Broyden. At the starting point, the Jacobian is approximated by forward differences, but these are not used again until the rank-1 method fails to produce satisfactory progress. For more details see Powell (1970).

## References

Moré J J, Garbow B S and Hillstrom K E (1980) User guide for MINPACK-1 Technical Report ANL-80-74 Argonne National Laboratory
Powell M J D (1970) A hybrid method for nonlinear algebraic equations Numerical Methods for Nonlinear Algebraic Equations (ed P Rabinowitz) Gordon and Breach

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{fcn}$ – function handle or string containing name of m-file
fcn must return the values of the functions ${f}_{i}$ at a point $x$, unless ${\mathbf{iflag}}=0$ on entry to nag_roots_sys_func_expert (c05qc).
[fvec, user, iflag] = fcn(n, x, fvec, user, iflag)

Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
$n$, the number of equations.
2:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
The components of the point $x$ at which the functions must be evaluated.
3:     $\mathrm{fvec}\left({\mathbf{n}}\right)$ – double array
If ${\mathbf{iflag}}=0$, fvec contains the function values ${f}_{i}\left(x\right)$ and must not be changed.
4:     $\mathrm{user}$ – Any MATLAB object
fcn is called from nag_roots_sys_func_expert (c05qc) with the object supplied to nag_roots_sys_func_expert (c05qc).
5:     $\mathrm{iflag}$int64int32nag_int scalar
${\mathbf{iflag}}\ge 0$.
${\mathbf{iflag}}=0$
x and fvec are available for printing (see nprint).
${\mathbf{iflag}}>0$
fvec must be updated.

Output Parameters

1:     $\mathrm{fvec}\left({\mathbf{n}}\right)$ – double array
If ${\mathbf{iflag}}>0$ on entry, fvec must contain the function values ${f}_{i}\left(x\right)$ (unless iflag is set to a negative value by fcn).
2:     $\mathrm{user}$ – Any MATLAB object
3:     $\mathrm{iflag}$int64int32nag_int scalar
In general, iflag should not be reset by fcn. If, however, you wish to terminate execution (perhaps because some illegal point x has been reached), then iflag should be set to a negative integer.
2:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
An initial guess at the solution vector.
3:     $\mathrm{ml}$int64int32nag_int scalar
The number of subdiagonals within the band of the Jacobian matrix. (If the Jacobian is not banded, or you are unsure, set ${\mathbf{ml}}={\mathbf{n}}-1$.)
Constraint: ${\mathbf{ml}}\ge 0$.
4:     $\mathrm{mu}$int64int32nag_int scalar
The number of superdiagonals within the band of the Jacobian matrix. (If the Jacobian is not banded, or you are unsure, set ${\mathbf{mu}}={\mathbf{n}}-1$.)
Constraint: ${\mathbf{mu}}\ge 0$.
5:     $\mathrm{mode}$int64int32nag_int scalar
Indicates whether or not you have provided scaling factors in diag.
If ${\mathbf{mode}}=2$ the scaling must have been specified in diag.
Otherwise, if ${\mathbf{mode}}=1$, the variables will be scaled internally.
Constraint: ${\mathbf{mode}}=1$ or $2$.
6:     $\mathrm{diag}\left({\mathbf{n}}\right)$ – double array
If ${\mathbf{mode}}=2$, diag must contain multiplicative scale factors for the variables.
If ${\mathbf{mode}}=1$, diag need not be set.
Constraint: if ${\mathbf{mode}}=2$, ${\mathbf{diag}}\left(\mathit{i}\right)>0.0$, for $\mathit{i}=1,2,\dots ,n$.
7:     $\mathrm{nprint}$int64int32nag_int scalar
Indicates whether (and how often) special calls to fcn, with iflag set to $0$, are to be made for printing purposes.
${\mathbf{nprint}}\le 0$
${\mathbf{nprint}}>0$
fcn is called at the beginning of the first iteration, every nprint iterations thereafter and immediately before the return from nag_roots_sys_func_expert (c05qc).

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the arrays x, diag. (An error is raised if these dimensions are not equal.)
$n$, the number of equations.
Constraint: ${\mathbf{n}}>0$.
2:     $\mathrm{xtol}$ – double scalar
Suggested value: $\sqrt{\epsilon }$, where $\epsilon$ is the machine precision returned by nag_machine_precision (x02aj).
Default: $\sqrt{\mathbit{machine precision}}$
The accuracy in x to which the solution is required.
Constraint: ${\mathbf{xtol}}\ge 0.0$.
3:     $\mathrm{maxfev}$int64int32nag_int scalar
Default: $200×\left({\mathbf{n}}+1\right)$
The maximum number of calls to fcn with ${\mathbf{iflag}}\ne 0$. nag_roots_sys_func_expert (c05qc) will exit with ${\mathbf{ifail}}={\mathbf{2}}$, if, at the end of an iteration, the number of calls to fcn exceeds maxfev.
Constraint: ${\mathbf{maxfev}}>0$.
4:     $\mathrm{epsfcn}$ – double scalar
Default: $0.0$
A rough estimate of the largest relative error in the functions. It is used in determining a suitable step for a forward difference approximation to the Jacobian. If epsfcn is less than machine precision (returned by nag_machine_precision (x02aj)) then machine precision is used. Consequently a value of $0.0$ will often be suitable.
5:     $\mathrm{factor}$ – double scalar
Default: $100.0$
A quantity to be used in determining the initial step bound. In most cases, factor should lie between $0.1$ and $100.0$. (The step bound is ${\mathbf{factor}}×{‖{\mathbf{diag}}×{\mathbf{x}}‖}_{2}$ if this is nonzero; otherwise the bound is factor.)
Constraint: ${\mathbf{factor}}>0.0$.
6:     $\mathrm{user}$ – Any MATLAB object
user is not used by nag_roots_sys_func_expert (c05qc), but is passed to fcn. Note that for large objects it may be more efficient to use a global variable which is accessible from the m-files than to use user.

### Output Parameters

1:     $\mathrm{x}\left({\mathbf{n}}\right)$ – double array
The final estimate of the solution vector.
2:     $\mathrm{fvec}\left({\mathbf{n}}\right)$ – double array
The function values at the final point returned in x.
3:     $\mathrm{diag}\left({\mathbf{n}}\right)$ – double array
The scale factors actually used (computed internally if ${\mathbf{mode}}=1$).
4:     $\mathrm{nfev}$int64int32nag_int scalar
The number of calls made to fcn with ${\mathbf{iflag}}>0$.
5:     $\mathrm{fjac}\left({\mathbf{n}},{\mathbf{n}}\right)$ – double array
The orthogonal matrix $Q$ produced by the $QR$ factorization of the final approximate Jacobian.
6:     $\mathrm{r}\left({\mathbf{n}}×\left({\mathbf{n}}+1\right)/2\right)$ – double array
The upper triangular matrix $R$ produced by the $QR$ factorization of the final approximate Jacobian, stored row-wise.
7:     $\mathrm{qtf}\left({\mathbf{n}}\right)$ – double array
The vector ${Q}^{\mathrm{T}}f$.
8:     $\mathrm{user}$ – Any MATLAB object
9:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

W  ${\mathbf{ifail}}=2$
There have been at least maxfev calls to fcn.
W  ${\mathbf{ifail}}=3$
No further improvement in the solution is possible.
W  ${\mathbf{ifail}}=4$
The iteration is not making good progress, as measured by the improvement from the last $_$ Jacobian evaluations.
W  ${\mathbf{ifail}}=5$
The iteration is not making good progress, as measured by the improvement from the last $_$ iterations.
W  ${\mathbf{ifail}}=6$
iflag was set negative in fcn.
${\mathbf{ifail}}=11$
Constraint: ${\mathbf{n}}>0$.
${\mathbf{ifail}}=12$
Constraint: ${\mathbf{xtol}}\ge 0.0$.
${\mathbf{ifail}}=13$
Constraint: ${\mathbf{mode}}=1$ or $2$.
${\mathbf{ifail}}=14$
Constraint: ${\mathbf{factor}}>0.0$.
${\mathbf{ifail}}=15$
On entry, ${\mathbf{mode}}=2$ and diag contained a non-positive element.
${\mathbf{ifail}}=16$
Constraint: ${\mathbf{ml}}\ge 0$.
${\mathbf{ifail}}=17$
Constraint: ${\mathbf{mu}}\ge 0$.
${\mathbf{ifail}}=18$
Constraint: ${\mathbf{maxfev}}>0$.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
A value of ${\mathbf{ifail}}={\mathbf{4}}$ or ${\mathbf{5}}$ may indicate that the system does not have a zero, or that the solution is very close to the origin (see Accuracy). Otherwise, rerunning nag_roots_sys_func_expert (c05qc) from a different starting point may avoid the region of difficulty.

## Accuracy

If $\stackrel{^}{x}$ is the true solution and $D$ denotes the diagonal matrix whose entries are defined by the array diag, then nag_roots_sys_func_expert (c05qc) tries to ensure that
 $D x-x^ 2 ≤ xtol × D x^ 2 .$
If this condition is satisfied with ${\mathbf{xtol}}={10}^{-k}$, then the larger components of $Dx$ have $k$ significant decimal digits. There is a danger that the smaller components of $Dx$ may have large relative errors, but the fast rate of convergence of nag_roots_sys_func_expert (c05qc) usually obviates this possibility.
If xtol is less than machine precision and the above test is satisfied with the machine precision in place of xtol, then the function exits with ${\mathbf{ifail}}={\mathbf{3}}$.
Note:  this convergence test is based purely on relative error, and may not indicate convergence if the solution is very close to the origin.
The convergence test assumes that the functions are reasonably well behaved. If this condition is not satisfied, then nag_roots_sys_func_expert (c05qc) may incorrectly indicate convergence. The validity of the answer can be checked, for example, by rerunning nag_roots_sys_func_expert (c05qc) with a lower value for xtol.

Local workspace arrays of fixed lengths are allocated internally by nag_roots_sys_func_expert (c05qc). The total size of these arrays amounts to $4×n$ double elements.
The time required by nag_roots_sys_func_expert (c05qc) to solve a given problem depends on $n$, the behaviour of the functions, the accuracy requested and the starting point. The number of arithmetic operations executed by nag_roots_sys_func_expert (c05qc) to process each evaluation of the functions is approximately $11.5×{n}^{2}$. The timing of nag_roots_sys_func_expert (c05qc) is strongly influenced by the time spent evaluating the functions.
Ideally the problem should be scaled so that, at the solution, the function values are of comparable magnitude.
The number of function evaluations required to evaluate the Jacobian may be reduced if you can specify ml and mu accurately.

## Example

This example determines the values ${x}_{1},\dots ,{x}_{9}$ which satisfy the tridiagonal equations:
 $3-2x1x1-2x2 = -1, -xi-1+3-2xixi-2xi+1 = -1, i=2,3,…,8 -x8+3-2x9x9 = -1.$
```function c05qc_example

fprintf('c05qc example results\n\n');

ml = int64(1);
mu = int64(1);
mode = int64(2);
diag = ones(9, 1);
nprint = int64(0);
% The following starting values provide a rough solution.
x = -ones(9, 1);
[xOut, fvec, diagOut, nfev, fjac, r, qtf, user, ifail] = ...
c05qc(@fcn, x, ml, mu, mode, diag, nprint);
switch ifail
case {0}
fprintf('\nFinal 2-norm of the residuals  = %12.4e\n', norm(fvec));
fprintf('\nNumber of function evaluations = %d\n', nfev);
fprintf('\nFinal approximate solution\n');
disp(xOut);
case {2, 3, a, 54}
fprintf('\nApproximate solution\n');
disp(xOut);
end

function [fvec, user, iflag] = fcn(n, x, fvec, user, iflag)
if iflag ~= 0
fvec = zeros(n, 1);
fvec(1:n) = (3.0-2.0.*x).*x + 1.0;
fvec(2:n) = fvec(2:n) - x(1:(n-1));
fvec(1:(n-1)) = fvec(1:(n-1)) - 2.0.*x(2:n);
end
```
```c05qc example results

Final 2-norm of the residuals  =   1.1926e-08

Number of function evaluations = 14

Final approximate solution
-0.5707
-0.6816
-0.7017
-0.7042
-0.7014
-0.6919
-0.6658
-0.5960
-0.4164

```