d02agf solves a two-point boundary value problem for a system of ordinary differential equations, using initial value techniques and Newton iteration; it generalizes d02haf to include the case where parameters other than boundary values are to be determined.
The routine may be called by the names d02agf or nagf_ode_bvp_shoot_genpar_intern.
3Description
d02agf solves a two-point boundary value problem by determining the unknown parameters ${p}_{1},{p}_{2},\dots ,{p}_{{\mathit{n}}_{1}}$ of the problem. These parameters may be, but need not be, boundary values (as they are in d02haf); they may include eigenvalue parameters in the coefficients of the differential equations, length of the range of integration, etc. The notation and methods used are similar to those of d02haf and you are advised to study this first. (There the parameters ${p}_{1},{p}_{2},\dots ,{p}_{{\mathit{n}}_{1}}$ correspond to the unknown boundary conditions.) It is assumed that we have a system of $\mathit{n}$ first-order ordinary differential equations of the form
and that derivatives ${f}_{i}$ are evaluated by aux. The system, including the boundary conditions given by bcaux, and the range of integration and matching point, $r$, given by raaux, involves the ${\mathit{n}}_{1}$ unknown parameters ${p}_{1},{p}_{2},\dots ,{p}_{{\mathit{n}}_{1}}$ which are to be determined, and for which initial estimates must be supplied. The number of unknown parameters ${\mathit{n}}_{1}$ must not exceed the number of equations $\mathit{n}$. If ${\mathit{n}}_{1}<\mathit{n}$, we assume that $(\mathit{n}-{\mathit{n}}_{1})$ equations of the system are not involved in the matching process. These are usually referred to as ‘driving equations’; they are independent of the parameters and of the solutions of the other ${\mathit{n}}_{1}$ equations. In numbering the equations for aux, the driving equations must be put last.
The estimated values of the parameters are corrected by a form of Newton iteration. The Newton correction on each iteration is calculated using a matrix whose $(i,j)$th element depends on the derivative of the $i$th component of the solution, ${y}_{i}$, with respect to the $j$th parameter, ${p}_{j}$. This matrix is calculated by a simple numerical differentiation technique which requires ${\mathit{n}}_{1}$ evaluations of the differential system.
4References
None.
5Arguments
You are strongly recommended to read Sections 3 and 9 in conjunction with this section.
1: $\mathbf{h}$ – Real (Kind=nag_wp)Input/Output
On entry: h must be set to an estimate of the step size, $h$, needed for integration.
On exit: the last step length used.
2: $\mathbf{e}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: ${\mathbf{e}}\left(i\right)$ must be set to a small quantity to control the $i$th solution component. The element ${\mathbf{e}}\left(i\right)$ is used:
(i)in the bound on the local error in the $i$th component of the solution ${y}_{i}$ during integration,
(ii)in the convergence test on the $i$th component of the solution ${y}_{i}$ at the matching point in the Newton iteration.
The elements ${\mathbf{e}}\left(i\right)$ should not be chosen too small. They should usually be several orders of magnitude larger than machine precision.
3: $\mathbf{parerr}\left({\mathbf{n1}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: ${\mathbf{parerr}}\left(i\right)$ must be set to a small quantity to control the $i$th parameter component. The element ${\mathbf{parerr}}\left(i\right)$ is used:
(i)in the convergence test on the $i$th parameter in the Newton iteration,
(ii)in perturbing the $i$th parameter when approximating the derivatives of the components of the solution with respect to the $i$th parameter, for use in the Newton iteration.
The elements ${\mathbf{parerr}}\left(i\right)$ should not be chosen too small. They should usually be several orders of magnitude larger than machine precision.
4: $\mathbf{param}\left({\mathbf{n1}}\right)$ – Real (Kind=nag_wp) arrayInput/Output
On entry: ${\mathbf{param}}\left(\mathit{i}\right)$ must be set to an estimate for the $\mathit{i}$th parameter, ${p}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{\mathbf{n1}}$.
On exit: the corrected value for the $i$th parameter, unless an error has occurred, when it contains the last calculated value of the parameter (possibly perturbed by ${\mathbf{parerr}}\left(i\right)\times (1+\left|{\mathbf{param}}\left(i\right)\right|)$ if the error occurred when calculating the approximate derivatives).
5: $\mathbf{c}({\mathbf{m1}},{\mathbf{n}})$ – Real (Kind=nag_wp) arrayOutput
On exit: the solution when ${\mathbf{m1}}>1$ (see m1).
If ${\mathbf{m1}}=1$, the elements of c are not used.
6: $\mathbf{n}$ – IntegerInput
On entry: $\mathit{n}$, the total number of differential equations.
7: $\mathbf{n1}$ – IntegerInput
On entry: ${\mathit{n}}_{1}$, the number of parameters.
If ${\mathbf{n1}}<{\mathbf{n}}$, the last ${\mathbf{n}}-{\mathbf{n1}}$ differential equations (in aux) are driving equations (see Section 3).
Constraint:
${\mathbf{n1}}\le {\mathbf{n}}$.
8: $\mathbf{m1}$ – IntegerInput
On entry: determines whether or not the final solution is computed as well as the parameter values.
${\mathbf{m1}}=1$
The final solution is not calculated;
${\mathbf{m1}}>1$
The final values of the solution at interval (length of range)/$({\mathbf{m1}}-1)$ are calculated and stored sequentially in the array c starting with the values of ${y}_{i}$ evaluated at the first end point (see raaux) stored in ${\mathbf{c}}(1,i)$.
9: $\mathbf{aux}$ – Subroutine, supplied by the user.External Procedure
aux must evaluate the functions ${f}_{i}$ (i.e., the derivatives ${y}_{i}^{\prime}$) for given values of its arguments, $x,{y}_{1},\dots ,{y}_{\mathit{n}}$, ${p}_{1},\dots ,{p}_{{\mathit{n}}_{1}}\text{.}$
In the description of the arguments of d02agf below, $\mathit{n}$ and $\mathit{n1}$ denote the numerical values of n and n1 in the call of d02agf.
1: $\mathbf{f}\left(*\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the value of
${f}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$.
2: $\mathbf{y}\left(*\right)$ – Real (Kind=nag_wp) arrayInput
On entry: ${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$, the value of the argument.
3: $\mathbf{x}$ – Real (Kind=nag_wp)Input
On entry: $x$, the value of the argument.
4: $\mathbf{param}\left(*\right)$ – Real (Kind=nag_wp) arrayInput
On entry: ${p}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{\mathit{n}}_{1}$, the value of the parameters.
aux must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which d02agf is called. Arguments denoted as Input must not be changed by this procedure.
Note:aux should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by d02agf. If your code inadvertently does return any NaNs or infinities, d02agf is likely to produce unexpected results.
10: $\mathbf{bcaux}$ – Subroutine, supplied by the user.External Procedure
bcaux must evaluate the values of ${y}_{i}$ at the end points of the range given the values of ${p}_{1},\dots ,{p}_{{\mathit{n}}_{1}}$.
In the description of the arguments of d02agf below, $\mathit{n}$ and $\mathit{n1}$ denote the numerical values of n and n1 in the call of d02agf.
1: $\mathbf{g0}\left(*\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the values
${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$, at the boundary point ${x}_{0}$ (see raaux).
2: $\mathbf{g1}\left(*\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: the values
${y}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$, at the boundary point ${x}_{1}$ (see raaux).
3: $\mathbf{param}\left(*\right)$ – Real (Kind=nag_wp) arrayInput
On entry: ${p}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,\mathit{n}$, the value of the parameters.
bcaux must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which d02agf is called. Arguments denoted as Input must not be changed by this procedure.
Note:bcaux should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by d02agf. If your code inadvertently does return any NaNs or infinities, d02agf is likely to produce unexpected results.
11: $\mathbf{raaux}$ – Subroutine, supplied by the user.External Procedure
raaux must evaluate the end points, ${x}_{0}$ and ${x}_{1}$, of the range and the matching point, $r$, given the values ${p}_{1},{p}_{2},\dots ,{p}_{{\mathit{n}}_{1}}$.
In the description of the arguments of d02agf below, $\mathit{n1}$ denotes the numerical value of n1 in the call of d02agf.
1: $\mathbf{x0}$ – Real (Kind=nag_wp)Output
On exit: must contain the left-hand end of the range, ${x}_{0}$.
2: $\mathbf{x1}$ – Real (Kind=nag_wp)Output
On exit: must contain the right-hand end of the range ${x}_{1}$.
3: $\mathbf{r}$ – Real (Kind=nag_wp)Output
On exit: must contain the matching point, $r$.
4: $\mathbf{param}\left(*\right)$ – Real (Kind=nag_wp) arrayInput
On entry: ${p}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{\mathit{n}}_{1}$, the value of the parameters.
raaux must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which d02agf is called. Arguments denoted as Input must not be changed by this procedure.
Note:raaux should not return floating-point NaN (Not a Number) or infinity values, since these are not handled by d02agf. If your code inadvertently does return any NaNs or infinities, d02agf is likely to produce unexpected results.
12: $\mathbf{prsol}$ – Subroutine, supplied by the user.External Procedure
prsol is called at each iteration of the Newton method and can be used to print the current values of the parameters
${p}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{\mathit{n}}_{1}$, their errors, ${e}_{i}$, and the sum of squares of the errors at the matching point, $r$.
1: $\mathbf{param}\left({\mathbf{n1}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: ${p}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{\mathit{n}}_{1}$, the current value of the parameters.
2: $\mathbf{res}$ – Real (Kind=nag_wp)Input
On entry: the sum of squares of the errors in the arguments, $\sum _{i=1}^{{\mathit{n}}_{1}}}{e}_{i}^{2$.
3: $\mathbf{n1}$ – IntegerInput
On entry: ${\mathit{n}}_{1}$, the number of parameters.
4: $\mathbf{err}\left({\mathbf{n1}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the errors in the parameters,
${e}_{\mathit{i}}$, for $\mathit{i}=1,2,\dots ,{\mathit{n}}_{1}$.
prsol must either be a module subprogram USEd by, or declared as EXTERNAL in, the (sub)program from which d02agf is called. Arguments denoted as Input must not be changed by this procedure.
13: $\mathbf{mat}({\mathbf{n1}},{\mathbf{n1}})$ – Real (Kind=nag_wp) arrayWorkspace
14: $\mathbf{copy}(1,1)$ – Real (Kind=nag_wp) arrayInput
15: $\mathbf{wspace}({\mathbf{n}},9)$ – Real (Kind=nag_wp) arrayWorkspace
16: $\mathbf{wspac1}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayWorkspace
17: $\mathbf{wspac2}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayWorkspace
18: $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $\mathrm{-1}$ or $1$ to set behaviour on detection of an error; these values have no effect when no error is detected.
A value of $0$ causes the printing of an error message and program execution will be halted; otherwise program execution continues. A value of $\mathrm{-1}$ means that an error message is printed while a value of $1$ means that it is not.
If halting is not appropriate, the value $\mathrm{-1}$ or $1$ is recommended. If message printing is undesirable, then the value $1$ is recommended. Otherwise, the value $0$ is recommended. When the value $-\mathbf{1}$ or $\mathbf{1}$ is used it is essential to test the value of ifail on exit.
On exit: ${\mathbf{ifail}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).
6Error Indicators and Warnings
If on entry ${\mathbf{ifail}}=0$ or $\mathrm{-1}$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
Note: a further discussion of these errors and the steps which might be taken to correct them is given in Section 9.
${\mathbf{ifail}}=1$
On entry, ${\mathbf{n1}}=\u27e8\mathit{\text{value}}\u27e9$ and ${\mathbf{n}}=\u27e8\mathit{\text{value}}\u27e9$.
Constraint: ${\mathbf{n1}}\le {\mathbf{n}}$.
${\mathbf{ifail}}=2$
No further progress can be made when stepping to obtain a Jacobian update for the current parameter values. Step length ${\mathbf{h}}=\u27e8\mathit{\text{value}}\u27e9$.
${\mathbf{ifail}}=3$
Currently the matching point $r$ does not lie in range $[{x}_{0},{x}_{1}]$.
If ${x}_{0}$, ${x}_{1}$ or $r$ depend on the parameters then this may occur when care is not taken to avoid it. $r=\u27e8\mathit{\text{value}}\u27e9$, ${x}_{0}=\u27e8\mathit{\text{value}}\u27e9$ and ${x}_{1}=\u27e8\mathit{\text{value}}\u27e9$.
${\mathbf{ifail}}=4$
No further progress can be made when stepping to a solution corresponding to the current parameter values.
Step length ${\mathbf{h}}=\u27e8\mathit{\text{value}}\u27e9$.
The step length for integration h has halved more than $13$ times (or too many steps were needed to reach the end of the range of integration) in attempting to control the local truncation error whilst integrating to obtain the solution corresponding to the current values ${p}_{i}$. If, on failure, h has the sign of $r-{x}_{0}$ then failure has occurred whilst integrating from ${x}_{0}$ to $r$, otherwise it has occurred whilst integrating from ${x}_{1}$ to $r$.
${\mathbf{ifail}}=5$
The Jacobian for parameter corrections is singular.
${\mathbf{ifail}}=6$
The Newton method failed to converge while updating parameter values.
${\mathbf{ifail}}=7$
The Newton method has not converged after $12$ iterations while updating parameter values.
${\mathbf{ifail}}=-99$
An unexpected error has been triggered by this routine. Please
contact NAG.
See Section 7 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 8 in the Introduction to the NAG Library FL Interface for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 9 in the Introduction to the NAG Library FL Interface for further information.
7Accuracy
If the process converges, the accuracy to which the unknown parameters are determined is usually close to that specified by you; and the solution, if requested, is usually determined to the accuracy specified.
8Parallelism and Performance
Background information to multithreading can be found in the Multithreading documentation.
d02agf is threaded by NAG for parallel execution in multithreaded implementations of the NAG Library.
d02agf makes calls to BLAS and/or LAPACK routines, which may be threaded within the vendor library used by this implementation. Consult the documentation for the vendor library for further information.
Please consult the X06 Chapter Introduction for information on how to control and interrogate the OpenMP environment used within this routine. Please also consult the Users' Note for your implementation for any additional implementation-specific information.
9Further Comments
The time taken by d02agf depends on the complexity of the system, and on the number of iterations required. In practice, integration of the differential equations is by far the most costly process involved.
There may be particular difficulty in integrating the differential equations in one direction (indicated by ${\mathbf{ifail}}={\mathbf{2}}$ or ${\mathbf{4}}$). The value of $r$ should be adjusted to avoid such difficulties.
If the matching point $r$ is at one of the end points ${x}_{0}$ or ${x}_{1}$ and some of the parameters are used only to determine the boundary values at this point, then good initial estimates for these parameters are not required, since they are completely determined by the routine.
Wherever they occur in the procedure, the error parameters contained in the arrays e and parerr are used in ‘mixed’ form; that is ${\mathbf{e}}\left(i\right)$ always occurs in expressions of the form ${\mathbf{e}}\left(i\right)\times (1+\left|{y}_{i}\right|)$, and ${\mathbf{parerr}}\left(i\right)$ always occurs in expressions of the form ${\mathbf{parerr}}\left(i\right)\times (1+\left|{p}_{i}\right|)$. Though not ideal for every application, it is expected that this mixture of absolute and relative error testing will be adequate for most purposes.
Note that convergenceisnotguaranteed. You are strongly advised to provide an output prsol in order to monitor the progress of the iteration. Failure of the Newton iteration to converge (see ${\mathbf{ifail}}={\mathbf{6}}$ or ${\mathbf{7}}$) usually results from poor starting approximations to the parameters, though occasionally such failures occur because the elements of one or both of the arrays parerr or e are too small. (It should be possible to distinguish these cases by studying the output from prsol.) Poor starting approximations can also result in the failure described under ${\mathbf{ifail}}={\mathbf{4}}$ and ${\mathbf{5}}$ in Section 6 (especially if these errors occur after some Newton iterations have been completed, that is, after two or more calls of prsol). More frequently, a singular matrix in the Newton method (monitored as ${\mathbf{ifail}}={\mathbf{5}}$) occurs because the mathematical problem has been posed incorrectly. The case ${\mathbf{ifail}}={\mathbf{4}}$ usually occurs because $h$ or $r$ has been poorly estimated, so these values should be checked first. If ${\mathbf{ifail}}={\mathbf{2}}$ is monitored, the solution ${y}_{1},{y}_{2},\dots ,{y}_{\mathit{n}}$ is sensitive to perturbations in the parameters ${p}_{i}$. Reduce the size of one or more values ${\mathbf{parerr}}\left(i\right)$ to reduce the perturbations. Since only one value ${p}_{i}$ is perturbed at any time when forming the matrix, the perturbation which is too large can be located by studying the final output from prsol and the values of the parameters returned by d02agf. If this change leads to other types of failure improve the initial values of ${p}_{i}$ by other means.
The computing time for integrating the differential equations can sometimes depend critically on the quality of the initial estimates for the parameters ${p}_{i}$. If it seems that too much computing time is required and, in particular, if the values ${\mathbf{err}}\left(i\right)$ (available on each call of prsol) are much larger than the expected values of the solution at the matching point $r$, then the coding of aux, bcaux and raaux should be checked for errors. If no errors can be found, an independent attempt should be made to improve the initial estimates for ${\mathbf{param}}\left(i\right)$.
The subroutine can be used to solve a very wide range of problems, for example:
(a)eigenvalue problems, including problems where the eigenvalue occurs in the boundary conditions;
(b)problems where the differential equations depend on some parameters which are to be determined so as to satisfy certain boundary conditions;
(c)problems where one of the end points of the range of integration is to be determined as the point where a variable ${y}_{i}$ takes a particular value;
(d)singular problems and problems on infinite ranges of integration where the values of the solution at ${x}_{0}$ or ${x}_{1}$ or both are determined by a power series or an asymptotic expansion (or a more complicated expression) and where some of the coefficients in the expression are to be determined; and
(e)differential equations with certain terms defined by other independent (driving) differential equations.
10Example
For this routine two examples are presented. There is a single example program for d02agf, with a main program and the code to solve the two example problems given in Example 1 (EX1) and Example 2 (EX2).
Example 1 (EX1)
This example finds the solution of the differential equation
near the origin (which is correct to the number of terms given in this case). Here ${p}_{1}$ is one of the parameters to be determined. We choose the range as $[0.1,16]$ and setting ${p}_{2}={y}^{\prime}\left(16\right)$, we can determine all the boundary conditions. We take the matching point to be $16$, the end of the range, and so a good initial guess for ${p}_{2}$ is not necessary. We write $y={\mathbf{y}}\left(1\right)$, ${y}^{\prime}={\mathbf{y}}\left(2\right)$, and estimate ${p}_{1}={\mathbf{param}}\left(1\right)=0.2$, ${p}_{2}={\mathbf{param}}\left(2\right)=0.0$.
Example 2 (EX2)
This example finds the gravitational constant ${p}_{1}$ and the range ${p}_{2}$ over which a projectile must be fired to hit the target with a given velocity. The differential equations are
We write $y={\mathbf{y}}\left(1\right)$, $v={\mathbf{y}}\left(2\right)$, $\varphi ={\mathbf{y}}\left(3\right)$, and we take the matching point $r={p}_{2}$. We estimate ${p}_{1}={\mathbf{param}}\left(1\right)=32$, ${p}_{2}={\mathbf{param}}\left(2\right)=6000$ and ${p}_{3}={\mathbf{param}}\left(3\right)=0.54$ (though this estimate is not important).