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_withdraw_sys_deriv_expert_old (c05pc)

## Purpose

nag_roots_withdraw_sys_deriv_expert (c05pc) is a comprehensive function that finds a solution of a system of nonlinear equations by a modification of the Powell hybrid method. You must provide the Jacobian.
Note: this function is scheduled to be withdrawn, please see c05pc in Advice on Replacement Calls for Withdrawn/Superseded Routines..

## Syntax

[x, fvec, fjac, diag, nfev, njev, r, qtf, ifail] = c05pc(fcn, x, diag, mode, nprint, 'n', n, 'xtol', xtol, 'maxfev', maxfev, 'factor', factor)
[x, fvec, fjac, diag, nfev, njev, r, qtf, ifail] = nag_roots_withdraw_sys_deriv_expert_old(fcn, x, diag, mode, nprint, 'n', n, 'xtol', xtol, 'maxfev', maxfev, 'factor', factor)
Note: the interface to this routine has changed since earlier releases of the toolbox:
Mark 23: lr no longer an input parameter
.

## Description

The system of equations is defined as:
 fi (x1,x2, … ,xn) = 0 ,   ​ i = 1, 2, … , n . $fi (x1,x2,…,xn) = 0 , ​ i= 1, 2, …, n .$
nag_roots_withdraw_sys_deriv_expert (c05pc) is based on the MINPACK routine HYBRJ (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 calculated, but it is not recalculated 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:     fcn – function handle or string containing name of m-file
Depending upon the value of iflag, fcn must either return the values of the functions fi ${f}_{i}$ at a point x$x$ or return the Jacobian at x$x$.
[fvec, fjac, iflag] = fcn(n, x, fvec, fjac, ldfjac, iflag)

Input Parameters

1:     n – int64int32nag_int scalar
n$n$, the number of equations.
2:     x(n) – double array
The components of the point x$x$ at which the functions or the Jacobian must be evaluated.
3:     fvec(n) – double array
If iflag = 0${\mathbf{iflag}}=0$ or 2$2$, fvec contains the function values fi(x) ${f}_{i}\left(x\right)$ and must not be changed.
4:     fjac(ldfjac,n) – double array
If iflag = 0${\mathbf{iflag}}=0$, fjac(i,j)${\mathbf{fjac}}\left(\mathit{i},\mathit{j}\right)$ contains the value of (fi)/(xj) $\frac{\partial {f}_{\mathit{i}}}{\partial {x}_{\mathit{j}}}$ at the point x$x$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$ and j = 1,2,,n$\mathit{j}=1,2,\dots ,n$. When iflag = 0${\mathbf{iflag}}=0$ or 1$1$, fjac must not be changed.
5:     ldfjac – int64int32nag_int scalar
The first dimension of the array fjac as declared in the (sub)program from which nag_roots_withdraw_sys_deriv_expert (c05pc) is called.
6:     iflag – int64int32nag_int scalar
iflag = 0${\mathbf{iflag}}=0$, 1$1$ or 2$2$.
iflag = 0 ${\mathbf{iflag}}=0$
x and fvec are available for printing (see nprint).
iflag = 1 ${\mathbf{iflag}}=1$
fvec is to be updated.
iflag = 2 ${\mathbf{iflag}}=2$
fjac is to be updated.

Output Parameters

1:     fvec(n) – double array
If iflag = 1 ${\mathbf{iflag}}=1$ on entry, fvec must contain the function values fi(x) ${f}_{i}\left(x\right)$ (unless iflag is set to a negative value by fcn).
2:     fjac(ldfjac,n) – double array
ldfjacn $\mathit{ldfjac}\ge {\mathbf{n}}$.
If iflag = 2 ${\mathbf{iflag}}=2$ on entry, fjac(i,j) ${\mathbf{fjac}}\left(\mathit{i},\mathit{j}\right)$ must contain the value of (fi)/(xj) $\frac{\partial {f}_{\mathit{i}}}{\partial {x}_{\mathit{j}}}$ at the point x$x$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$ and j = 1,2,,n$\mathit{j}=1,2,\dots ,n$, (unless iflag is set to a negative value by fcn).
3:     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. This value will be returned through ifail.
2:     x(n) – double array
n, the dimension of the array, must satisfy the constraint n > 0 ${\mathbf{n}}>0$.
An initial guess at the solution vector.
3:     diag(n) – double array
n, the dimension of the array, must satisfy the constraint n > 0 ${\mathbf{n}}>0$.
If mode = 2${\mathbf{mode}}=2$, diag must contain multiplicative scale factors for the variables.
If mode = 1${\mathbf{mode}}=1$, diag need not be set.
Constraint: if mode = 2${\mathbf{mode}}=2$, diag(i) > 0.0 ${\mathbf{diag}}\left(\mathit{i}\right)>0.0$, for i = 1,2,,n$\mathit{i}=1,2,\dots ,n$.
4:     mode – int64int32nag_int scalar
Indicates whether or not you have provided scaling factors in diag.
If mode = 2${\mathbf{mode}}=2$ the scaling must have been specified in diag.
Otherwise, the variables will be scaled internally.
5:     nprint – int64int32nag_int scalar
Indicates whether (and how often) special calls to fcn, with iflag set to 0$0$, are to be made for printing purposes.
nprint0 ${\mathbf{nprint}}\le 0$
nprint > 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_withdraw_sys_deriv_expert (c05pc).

### Optional Input Parameters

1:     n – int64int32nag_int scalar
Default: The dimension of the arrays x, diag. (An error is raised if these dimensions are not equal.)
On initial entry: n$n$, the number of equations.
Constraint: n > 0 ${\mathbf{n}}>0$.
2:     xtol – double scalar
The accuracy in x to which the solution is required.
Suggested value: sqrt(ε)$\sqrt{\epsilon }$, where ε$\epsilon$ is the machine precision returned by nag_machine_precision (x02aj).
Default: sqrt(machine precision) $\sqrt{\mathbit{machine precision}}$
Constraint: xtol0.0 ${\mathbf{xtol}}\ge 0.0$.
3:     maxfev – int64int32nag_int scalar
The maximum number of calls to fcn with iflag0 ${\mathbf{iflag}}\ne 0$. nag_roots_withdraw_sys_deriv_expert (c05pc) will exit with ${\mathbf{ifail}}={\mathbf{2}}$, if, at the end of an iteration, the number of calls to fcn exceeds maxfev.
Default: 100 × (n + 1) $100×\left({\mathbf{n}}+1\right)$
Constraint: maxfev > 0 ${\mathbf{maxfev}}>0$.
4:     factor – double scalar
A quantity to be used in determining the initial step bound. In most cases, factor should lie between 0.1$0.1$ and 100.0$100.0$. (The step bound is factor × diag × x2 ${\mathbf{factor}}×{‖{\mathbf{diag}}×{\mathbf{x}}‖}_{2}$ if this is nonzero; otherwise the bound is factor.)
Default: 100.0$100.0$
Constraint: factor > 0.0 ${\mathbf{factor}}>0.0$.

w ldfjac

### Output Parameters

1:     x(n) – double array
The final estimate of the solution vector.
2:     fvec(n) – double array
The function values at the final point returned in x.
3:     fjac(ldfjac,n) – double array
ldfjacn $\mathit{ldfjac}\ge {\mathbf{n}}$.
The orthogonal matrix Q$Q$ produced by the QR $QR$ factorisation of the final approximate Jacobian.
4:     diag(n) – double array
The scale factors actually used (computed internally if mode = 1${\mathbf{mode}}=1$).
5:     nfev – int64int32nag_int scalar
The number of calls made to fcn to evaluate the functions.
6:     njev – int64int32nag_int scalar
The number of calls made to fcn to evaluate the Jacobian.
7:     r(n × (n + 1) / 2${\mathbf{n}}×\left({\mathbf{n}}+1\right)/2$) – double array
The upper triangular matrix R$R$ produced by the QR $QR$ factorization of the final approximate Jacobian, stored row-wise.
8:     qtf(n) – double array
The vector QTf ${Q}^{\mathrm{T}}f$.
9:     ifail – int64int32nag_int scalar
${\mathrm{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 ifail < 0${\mathbf{ifail}}<0$
This indicates an exit from nag_roots_withdraw_sys_deriv_expert (c05pc) because you have set iflag negative in fcn. The value of ifail will be the same as your setting of iflag.
ifail = 1${\mathbf{ifail}}=1$
 On entry, n ≤ 0 ${\mathbf{n}}\le 0$, or xtol < 0.0 ${\mathbf{xtol}}<0.0$, or maxfev ≤ 0 ${\mathbf{maxfev}}\le 0$, or factor ≤ 0.0 ${\mathbf{factor}}\le 0.0$, or ldfjac < n $\mathit{ldfjac}<{\mathbf{n}}$, or mode = 2 ${\mathbf{mode}}=2$ and diag(i) ≤ 0.0 ${\mathbf{diag}}\left(i\right)\le 0.0$ for some i$i$, i = 1, 2, … , n $i=1,2,\dots ,{\mathbf{n}}$.
W ifail = 2${\mathbf{ifail}}=2$
There have been maxfev evaluations of fcn to evaluate the functions. Consider restarting the calculation from the final point held in x.
W ifail = 3${\mathbf{ifail}}=3$
No further improvement in the approximate solution x is possible; xtol is too small.
W ifail = 4${\mathbf{ifail}}=4$
The iteration is not making good progress, as measured by the improvement from the last five Jacobian evaluations.
W ifail = 5${\mathbf{ifail}}=5$
The iteration is not making good progress, as measured by the improvement from the last ten iterations.
ifail = 999${\mathbf{ifail}}=-999$
Internal memory allocation failed.
The values ${\mathbf{ifail}}={\mathbf{4}}$ and 5${\mathbf{5}}$ may indicate that the system does not have a zero, or that the solution is very close to the origin (see Section [Accuracy]). Otherwise, rerunning nag_roots_withdraw_sys_deriv_expert (c05pc) from a different starting point may avoid the region of difficulty.

## Accuracy

If $\stackrel{^}{x}$ is the true solution and D$D$ denotes the diagonal matrix whose entries are defined by the array diag, then nag_roots_withdraw_sys_deriv_expert (c05pc) tries to ensure that
 ‖D(x − x̂)‖2 ≤ xtol × ‖Dx̂‖2 . $‖ D (x-x^) ‖2 ≤ xtol × ‖ D x^ ‖2 .$
If this condition is satisfied with xtol = 10k ${\mathbf{xtol}}={10}^{-k}$, then the larger components of Dx $Dx$ have k$k$ significant decimal digits. There is a danger that the smaller components of Dx $Dx$ may have large relative errors, but the fast rate of convergence of nag_roots_withdraw_sys_deriv_expert (c05pc) 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 test assumes that the functions and the Jacobian are coded consistently and that the functions are reasonably well behaved. If these conditions are not satisfied, then nag_roots_withdraw_sys_deriv_expert (c05pc) may incorrectly indicate convergence. The coding of the Jacobian can be checked using nag_roots_withdraw_sys_deriv_check (c05za). If the Jacobian is coded correctly, then the validity of the answer can be checked by rerunning nag_roots_withdraw_sys_deriv_expert (c05pc) with a lower value for xtol.

Local workspace arrays of fixed lengths are allocated internally by nag_roots_withdraw_sys_deriv_expert (c05pc). The total size of these arrays amounts to 4 × n$4×{\mathbf{n}}$ double elements.
The time required by nag_roots_withdraw_sys_deriv_expert (c05pc) to solve a given problem depends on n$n$, the behaviour of the functions, the accuracy requested and the starting point. The number of arithmetic operations executed by nag_roots_withdraw_sys_deriv_expert (c05pc) is about 11.5 × n2 $11.5×{n}^{2}$ to process each evaluation of the functions and about 1.3 × n3 $1.3×{n}^{3}$ to process each evaluation of the Jacobian. Unless fcn can be evaluated quickly, the timing of nag_roots_withdraw_sys_deriv_expert (c05pc) will be strongly influenced by the time spent in fcn.
Ideally the problem should be scaled so that, at the solution, the function values are of comparable magnitude.

## Example

```function nag_roots_withdraw_sys_deriv_expert_old_example
x = [-1; -1; -1; -1; -1; -1; -1; -1; -1];
diag = [1; 1; 1; 1; 1; 1; 1; 1; 1];
mode = int64(2);
nprint = int64(0);
[xOut, fvec, fjac, diagOut, nfev, njev, r, qtf, ifail] = ...
nag_roots_withdraw_sys_deriv_expert_old(@fcn, x, diag, mode, nprint)

function [fvec, fjac, iflag] = fcn(n,x,fvec,fjac,ldfjac,iflag)

if (iflag ~= 2)
for k = 1:double(n)
fvec(k) = (3.0-2.0*x(k))*x(k) + 1.0;
if (k > 1)
fvec(k) = fvec(k) - x(k-1);
end
if (k < n)
fvec(k) = fvec(k) - 2.0*x(k+1);
end
end
else
fjac = zeros(n,n);
for k = 1:double(n)
fjac(k,k) = 3.0 - 4.0*x(k);
if (k > 1)
fjac(k,k-1) = -1.0;
end
if (k < n)
fjac(k,k+1) = -2.0;
end
end
end
```
```

xOut =

-0.5707
-0.6816
-0.7017
-0.7042
-0.7014
-0.6919
-0.6658
-0.5960
-0.4164

fvec =

1.0e-08 *

0.6560
-0.4175
-0.5193
-0.2396
0.2022
0.4818
0.2579
-0.3884
-0.0136

fjac =

-0.9691   -0.2148   -0.0209    0.0470    0.0611    0.0339   -0.0188   -0.0678    0.0471
0.2268   -0.9561   -0.1558    0.0078    0.0423    0.0328   -0.0064   -0.0621    0.0582
-0.0174    0.1584   -0.9720   -0.1533   -0.0206    0.0090    0.0087   -0.0254    0.0720
-0.0478   -0.0482    0.1554   -0.9653   -0.1728   -0.0306    0.0139    0.0072    0.0918
-0.0414   -0.0486   -0.0103    0.1910   -0.9579   -0.1638   -0.0068    0.0214    0.1198
-0.0072   -0.0143   -0.0058    0.0102    0.1882   -0.9588   -0.1373   -0.0024    0.1614
0.0361    0.0408    0.0197    0.0011   -0.0011    0.1731   -0.9455   -0.1429    0.2288
0.0591    0.0849    0.0606    0.0302    0.0175    0.0320    0.2366   -0.8900    0.3679
0.0164    0.0304    0.0474    0.0704    0.1040    0.1402    0.1748    0.4218    0.8676

diagOut =

1
1
1
1
1
1
1
1
1

nfev =

11

njev =

1

r =

-5.9002
4.1274
-0.4204
-0.4522
-0.5225
-0.3437
-0.0653
0.0185
-0.5519
-6.0563
3.2570
-0.5570
-0.3043
-0.2274
-0.0452
0.1224
-0.3490
-6.4766
3.2423
-0.2317
-0.0327
-0.0923
0.0258
0.0501
-6.3881
3.5140
-0.1806
-0.1332
-0.1071
0.4337
-6.2931
3.4096
-0.4194
-0.1511
0.7698
-6.4309
3.1398
-0.3188
0.9972
-6.5080
3.4679
0.7938
-6.0582
4.8522
3.8733

qtf =

1.0e-07 *

-0.3574
0.0823
0.2291
0.1836
0.0005
-0.2262
-0.2399
0.1980
-0.0074

ifail =

0

```
```function c05pc_example
x = [-1; -1; -1; -1; -1; -1; -1; -1; -1];
diag = [1; 1; 1; 1; 1; 1; 1; 1; 1];
mode = int64(2);
nprint = int64(0);
[xOut, fvec, fjac, diagOut, nfev, njev, r, qtf, ifail] = ...
c05pc(@fcn, x, diag, mode, nprint)

function [fvec, fjac, iflag] = fcn(n,x,fvec,fjac,ldfjac,iflag)

if (iflag ~= 2)
for k = 1:double(n)
fvec(k) = (3.0-2.0*x(k))*x(k) + 1.0;
if (k > 1)
fvec(k) = fvec(k) - x(k-1);
end
if (k < n)
fvec(k) = fvec(k) - 2.0*x(k+1);
end
end
else
fjac = zeros(n,n);
for k = 1:double(n)
fjac(k,k) = 3.0 - 4.0*x(k);
if (k > 1)
fjac(k,k-1) = -1.0;
end
if (k < n)
fjac(k,k+1) = -2.0;
end
end
end
```
```

xOut =

-0.5707
-0.6816
-0.7017
-0.7042
-0.7014
-0.6919
-0.6658
-0.5960
-0.4164

fvec =

1.0e-08 *

0.6560
-0.4175
-0.5193
-0.2396
0.2022
0.4818
0.2579
-0.3884
-0.0136

fjac =

-0.9691   -0.2148   -0.0209    0.0470    0.0611    0.0339   -0.0188   -0.0678    0.0471
0.2268   -0.9561   -0.1558    0.0078    0.0423    0.0328   -0.0064   -0.0621    0.0582
-0.0174    0.1584   -0.9720   -0.1533   -0.0206    0.0090    0.0087   -0.0254    0.0720
-0.0478   -0.0482    0.1554   -0.9653   -0.1728   -0.0306    0.0139    0.0072    0.0918
-0.0414   -0.0486   -0.0103    0.1910   -0.9579   -0.1638   -0.0068    0.0214    0.1198
-0.0072   -0.0143   -0.0058    0.0102    0.1882   -0.9588   -0.1373   -0.0024    0.1614
0.0361    0.0408    0.0197    0.0011   -0.0011    0.1731   -0.9455   -0.1429    0.2288
0.0591    0.0849    0.0606    0.0302    0.0175    0.0320    0.2366   -0.8900    0.3679
0.0164    0.0304    0.0474    0.0704    0.1040    0.1402    0.1748    0.4218    0.8676

diagOut =

1
1
1
1
1
1
1
1
1

nfev =

11

njev =

1

r =

-5.9002
4.1274
-0.4204
-0.4522
-0.5225
-0.3437
-0.0653
0.0185
-0.5519
-6.0563
3.2570
-0.5570
-0.3043
-0.2274
-0.0452
0.1224
-0.3490
-6.4766
3.2423
-0.2317
-0.0327
-0.0923
0.0258
0.0501
-6.3881
3.5140
-0.1806
-0.1332
-0.1071
0.4337
-6.2931
3.4096
-0.4194
-0.1511
0.7698
-6.4309
3.1398
-0.3188
0.9972
-6.5080
3.4679
0.7938
-6.0582
4.8522
3.8733

qtf =

1.0e-07 *

-0.3574
0.0823
0.2291
0.1836
0.0005
-0.2262
-0.2399
0.1980
-0.0074

ifail =

0

```