hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_roots_sys_deriv_expert (c05rc)

Purpose

nag_roots_sys_deriv_expert (c05rc) 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.

Syntax

[x, fvec, fjac, diag, nfev, njev, r, qtf, user, ifail] = c05rc(fcn, x, mode, diag, nprint, 'n', n, 'xtol', xtol, 'maxfev', maxfev, 'factor', factor, 'user', user)
[x, fvec, fjac, diag, nfev, njev, r, qtf, user, ifail] = nag_roots_sys_deriv_expert(fcn, x, mode, diag, nprint, 'n', n, 'xtol', xtol, 'maxfev', maxfev, 'factor', factor, 'user', user)

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_sys_deriv_expert (c05rc) 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 requested, but it is not asked for 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:     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 fi  at a point xx or return the Jacobian at xx.
[fvec, fjac, user, iflag] = fcn(n, x, fvec, fjac, user, iflag)

Input Parameters

1:     n – int64int32nag_int scalar
nn, the number of equations.
2:     x(n) – double array
The components of the point xx at which the functions or the Jacobian must be evaluated.
3:     fvec(n) – double array
If iflag = 0iflag=0 or 22, fvec contains the function values fi(x) fi(x)  and must not be changed.
4:     fjac(n,n) – double array
If iflag = 0iflag=0, fjac(i,j)fjacij contains the value of (fi)/(xj) fi xj  at the point xx, for i = 1,2,,ni=1,2,,n and j = 1,2,,nj=1,2,,n. When iflag = 0iflag=0 or 11, fjac must not be changed.
5:     user – Any MATLAB object
fcn is called from nag_roots_sys_deriv_expert (c05rc) with the object supplied to nag_roots_sys_deriv_expert (c05rc).
6:     iflag – int64int32nag_int scalar
iflag = 0iflag=0, 11 or 22.
iflag = 0 iflag=0
x, fvec and fjac are available for printing (see nprint).
iflag = 1 iflag=1
fvec is to be updated.
iflag = 2 iflag=2
fjac is to be updated.

Output Parameters

1:     fvec(n) – double array
If iflag = 1 iflag=1 on entry, fvec must contain the function values fi(x) fi(x) (unless iflag is set to a negative value by fcn).
2:     fjac(n,n) – double array
If iflag = 2 iflag=2 on entry, fjac(i,j) fjacij must contain the value of (fi)/(xj) fi xj at the point xx, for i = 1,2,,ni=1,2,,n and j = 1,2,,nj=1,2,,n, (unless iflag is set to a negative value by fcn).
3:     user – Any MATLAB object
4:     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 value.
2:     x(n) – double array
n, the dimension of the array, must satisfy the constraint n > 0 n>0 .
An initial guess at the solution vector.
3:     mode – int64int32nag_int scalar
Indicates whether or not you have provided scaling factors in diag.
If mode = 2mode=2 the scaling must have been specified in diag.
Otherwise, if mode = 1mode=1, the variables will be scaled internally.
Constraint: mode = 1mode=1 or 22.
4:     diag(n) – double array
n, the dimension of the array, must satisfy the constraint n > 0 n>0 .
If mode = 2mode=2, diag must contain multiplicative scale factors for the variables.
If mode = 1mode=1, diag need not be set.
Constraint: if mode = 2mode=2, diag(i) > 0.0 diagi>0.0 , for i = 1,2,,ni=1,2,,n.
5:     nprint – int64int32nag_int scalar
Indicates whether (and how often) special calls to fcn, with iflag set to 00, are to be made for printing purposes.
nprint0 nprint0
No calls are made.
nprint > 0 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_deriv_expert (c05rc).

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.)
nn, the number of equations.
Constraint: n > 0 n>0 .
2:     xtol – double scalar
The accuracy in x to which the solution is required.
Suggested value: sqrt(ε)ε, where εε is the machine precision returned by nag_machine_precision (x02aj).
Default: sqrt(machine precision) machine precision
Constraint: xtol0.0 xtol0.0 .
3:     maxfev – int64int32nag_int scalar
The maximum number of calls to fcn with iflag0 iflag0 . nag_roots_sys_deriv_expert (c05rc) will exit with ifail = 2ifail=2, if, at the end of an iteration, the number of calls to fcn exceeds maxfev.
Default: 100 × (n + 1) 100×(n+1)
Constraint: maxfev > 0 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.10.1 and 100.0100.0. (The step bound is factor × diag × x2 factor×diag×x2  if this is nonzero; otherwise the bound is factor.)
Default: 100.0100.0
Constraint: factor > 0.0 factor>0.0 .
5:     user – Any MATLAB object
user is not used by nag_roots_sys_deriv_expert (c05rc), 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.

Input Parameters Omitted from the MATLAB Interface

iuser ruser

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(n,n) – double array
The orthogonal matrix QQ produced by the QR QR factorization of the final approximate Jacobian.
4:     diag(n) – double array
The scale factors actually used (computed internally if mode = 1mode=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) / 2n×(n+1)/2) – double array
The upper triangular matrix RR produced by the QR QR factorization of the final approximate Jacobian, stored row-wise.
8:     qtf(n) – double array
The vector QTf QTf .
9:     user – Any MATLAB object
10:   ifail – int64int32nag_int scalar
ifail = 0ifail=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 = 2ifail=2
There have been at least maxfev calls to fcn.
W ifail = 3ifail=3
No further improvement in the solution is possible.
W ifail = 4ifail=4
The iteration is not making good progress, as measured by the improvement from the last __ Jacobian evaluations. This failure exit 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_sys_deriv_expert (c05rc) from a different starting point may avoid the region of difficulty.
W ifail = 5ifail=5
The iteration is not making good progress, as measured by the improvement from the last __ iterations. This failure exit 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_sys_deriv_expert (c05rc) from a different starting point may avoid the region of difficulty.
W ifail = 6ifail=6
iflag was set negative in fcn.
  ifail = 11ifail=11
Constraint: n > 0n>0.
  ifail = 12ifail=12
Constraint: xtol0.0xtol0.0.
  ifail = 13ifail=13
Constraint: mode = 1mode=1 or 22.
  ifail = 14ifail=14
Constraint: factor > 0.0factor>0.0.
  ifail = 15ifail=15
On entry, mode = 2mode=2 and diag contained a non-positive element.
  ifail = 18ifail=18
Constraint: maxfev > 0maxfev>0.
  ifail = 999ifail=-999
Dynamic memory allocation failed.

Accuracy

If x^  is the true solution and DD denotes the diagonal matrix whose entries are defined by the array diag, then nag_roots_sys_deriv_expert (c05rc) tries to ensure that
D(x)2 xtol × D2 .
D (x-x^) 2 xtol × D x^ 2 .
If this condition is satisfied with xtol = 10k xtol = 10-k , then the larger components of Dx Dx  have kk 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_sys_deriv_expert (c05rc) 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 ifail = 3ifail=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 and the Jacobian are coded consistently and that the functions are reasonably well behaved. If these conditions are not satisfied, then nag_roots_sys_deriv_expert (c05rc) may incorrectly indicate convergence. The coding of the Jacobian can be checked using nag_roots_sys_deriv_check (c05zd). If the Jacobian is coded correctly, then the validity of the answer can be checked by rerunning nag_roots_sys_deriv_expert (c05rc) with a lower value for xtol.

Further Comments

Local workspace arrays of fixed lengths are allocated internally by nag_roots_sys_deriv_expert (c05rc). The total size of these arrays amounts to 4 × n4×n double elements.
The time required by nag_roots_sys_deriv_expert (c05rc) to solve a given problem depends on nn, the behaviour of the functions, the accuracy requested and the starting point. The number of arithmetic operations executed by nag_roots_sys_deriv_expert (c05rc) is approximately 11.5 × n2 11.5×n2  to process each evaluation of the functions and approximately 1.3 × n3 1.3×n3  to process each evaluation of the Jacobian. The timing of nag_roots_sys_deriv_expert (c05rc) 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.

Example

function nag_roots_sys_deriv_expert_example
% The following starting values provide a rough solution.
x = -ones(9, 1);
diag = ones(9, 1);
mode = int64(2);
nprint = int64(0);
[xOut, fvec, fjac, diag, nfev, njev, r, qtf, user, ifail] = ...
    nag_roots_sys_deriv_expert(@fcn, x, mode, diag, nprint);

switch ifail
  case {0}
    fprintf('\nFinal 2-norm of the residuals = %12.4e\n', norm(fvec));
    fprintf('\nFinal approximate solution\n');
    disp(xOut);
  case {2, 3, 4}
    fprintf('\nApproximate solution\n');
    disp(xOut);
end




function [fvec, fjac, user, iflag] = fcn(n, x, fvec, fjac, user, iflag)
  coeff = [-1, 3, -2, -2, -1];
  nd = double(n); % Can't use 64 bit integers in loops
  if (iflag == 0)
    % We could print fvec and fjac if we wished
  elseif (iflag == 1)
    fvec(1:nd) = (coeff(2)+coeff(3)*x(1:nd)).*x(1:nd) - coeff(5);
    fvec(2:nd) = fvec(2:nd) + coeff(1)*x(1:(nd-1));
    fvec(1:(nd-1)) = fvec(1:(nd-1)) + coeff(4)*x(2:nd);
  else
    fjac = zeros(nd, nd);

    fjac(1,1) = coeff(2) + 2*coeff(3)*x(1);
    fjac(1,2) = coeff(4);
    for k = 2:nd-1
      fjac(k,k-1) = coeff(1);
      fjac(k,k) = coeff(2) + 2*coeff(3)*x(k);
      fjac(k,k+1) = coeff(4);
    end
    fjac(nd,nd-1) = coeff(1);
    fjac(nd,nd) = coeff(2) + 2*coeff(3)*x(nd);
  end
 

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

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


function c05rc_example
% The following starting values provide a rough solution.
x = -ones(9, 1);
diag = ones(9, 1);
mode = int64(2);
nprint = int64(0);
[xOut, fvec, fjac, diag, nfev, njev, r, qtf, user, ifail] = ...
    c05rc(@fcn, x, mode, diag, nprint);

switch ifail
  case {0}
    fprintf('\nFinal 2-norm of the residuals = %12.4e\n', norm(fvec));
    fprintf('\nFinal approximate solution\n');
    disp(xOut);
  case {2, 3, 4}
    fprintf('\nApproximate solution\n');
    disp(xOut);
end




function [fvec, fjac, user, iflag] = fcn(n, x, fvec, fjac, user, iflag)
  coeff = [-1, 3, -2, -2, -1];
  nd = double(n); % Can't use 64 bit integers in loops
  if (iflag == 0)
    % We could print fvec and fjac if we wished
  elseif (iflag == 1)
    fvec(1:nd) = (coeff(2)+coeff(3)*x(1:nd)).*x(1:nd) - coeff(5);
    fvec(2:nd) = fvec(2:nd) + coeff(1)*x(1:(nd-1));
    fvec(1:(nd-1)) = fvec(1:(nd-1)) + coeff(4)*x(2:nd);
  else
    fjac = zeros(nd, nd);

    fjac(1,1) = coeff(2) + 2*coeff(3)*x(1);
    fjac(1,2) = coeff(4);
    for k = 2:nd-1
      fjac(k,k-1) = coeff(1);
      fjac(k,k) = coeff(2) + 2*coeff(3)*x(k);
      fjac(k,k+1) = coeff(4);
    end
    fjac(nd,nd-1) = coeff(1);
    fjac(nd,nd) = coeff(2) + 2*coeff(3)*x(nd);
  end
 

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

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



PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2013