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_sum_invlaplace_crump (c06la)

## Purpose

nag_sum_invlaplace_crump (c06la) estimates values of the inverse Laplace transform of a given function using a Fourier series approximation. Real and imaginary parts of the function, and a bound on the exponential order of the inverse, are required.

## Syntax

[valinv, errest, nterms, na, alow, ahigh, nfeval, ifail] = c06la(fun, t, relerr, alphab, 'n', n, 'tfac', tfac, 'mxterm', mxterm)
[valinv, errest, nterms, na, alow, ahigh, nfeval, ifail] = nag_sum_invlaplace_crump(fun, t, relerr, alphab, 'n', n, 'tfac', tfac, 'mxterm', mxterm)

## Description

Given a function $F\left(p\right)$ defined for complex values of $p$, nag_sum_invlaplace_crump (c06la) estimates values of its inverse Laplace transform by Crump's method (see Crump (1976)). (For a definition of the Laplace transform and its inverse, see the C06 Chapter Introduction.)
Crump's method applies the epsilon algorithm (see Wynn (1956)) to the summation in Durbin's Fourier series approximation (see Durbin (1974))
 $f tj ≃ e atj τ 12 F a - ∑ k=1 ∞ Re F a + kπi τ cos⁡ kπtj τ - Im F a + kπi τ sin⁡ kπtj τ ,$
for $j=1,2,\dots ,n$, by choosing $a$ such that a prescribed relative error should be achieved. The method is modified slightly if $t=0.0$ so that an estimate of $f\left(0.0\right)$ can be obtained when it has a finite value. $\tau$ is calculated as ${t}_{\mathrm{fac}}×\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(0.01,{t}_{j}\right)$, where ${t}_{\mathrm{fac}}>0.5$. You specify ${t}_{\mathrm{fac}}$ and ${\alpha }_{b}$, an upper bound on the exponential order $\alpha$ of the inverse function $f\left(t\right)$. $\alpha$ has two alternative interpretations:
(i) $\alpha$ is the smallest number such that
 $ft ≤ m × expαt$
for large $t$,
(ii) $\alpha$ is the real part of the singularity of $F\left(p\right)$ with largest real part.
The method depends critically on the value of $\alpha$. See Further Comments for further details. The function calculates at least two different values of the argument $a$, such that $a>{\alpha }_{b}$, in an attempt to achieve the requested relative error and provide error estimates. The values of ${t}_{\mathit{j}}$, for $\mathit{j}=1,2,\dots ,n$, must be supplied in monotonically increasing order. The function calculates the values of the inverse function $f\left({t}_{j}\right)$ in decreasing order of $j$.

## References

Crump K S (1976) Numerical inversion of Laplace transforms using a Fourier series approximation J. Assoc. Comput. Mach. 23 89–96
Durbin F (1974) Numerical inversion of Laplace transforms: An efficient improvement to Dubner and Abate's method Comput. J. 17 371–376
Wynn P (1956) On a device for computing the ${e}_{m}\left({S}_{n}\right)$ transformation Math. Tables Aids Comput. 10 91–96

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{fun}$ – function handle or string containing name of m-file
fun must evaluate the real and imaginary parts of the function $F\left(p\right)$ for a given value of $p$.
[fr, fi] = fun(pr, pi)

Input Parameters

1:     $\mathrm{pr}$ – double scalar
2:     $\mathrm{pi}$ – double scalar
The real and imaginary parts of the argument $p$.

Output Parameters

1:     $\mathrm{fr}$ – double scalar
2:     $\mathrm{fi}$ – double scalar
The real and imaginary parts of the value $F\left(p\right)$.
2:     $\mathrm{t}\left({\mathbf{n}}\right)$ – double array
Each ${\mathbf{t}}\left(\mathit{j}\right)$ must specify a point at which the inverse Laplace transform is required , for $\mathit{j}=1,2,\dots ,n$.
Constraint: $0.0\le {\mathbf{t}}\left(1\right)<{\mathbf{t}}\left(2\right)<\cdots <{\mathbf{t}}\left(n\right)$.
3:     $\mathrm{relerr}$ – double scalar
The required relative error in the values of the inverse Laplace transform. If the absolute value of the inverse is less than relerr, then absolute accuracy is used instead. relerr must be in the range $0.0\le {\mathbf{relerr}}<1.0$. If relerr is set too small or to $0.0$, then the function uses a value sufficiently larger than machine precision.
4:     $\mathrm{alphab}$ – double scalar
${\alpha }_{b}$, an upper bound for $\alpha$ (see Description). Usually, ${\alpha }_{b}$ should be specified equal to, or slightly larger than, the value of $\alpha$. If ${\alpha }_{b}<\alpha$ then the prescribed accuracy may not be achieved or completely incorrect results may be obtained. If ${\alpha }_{b}$ is too large nag_sum_invlaplace_crump (c06la) will be inefficient and convergence may not be achieved.
Note:  it is as important to specify ${\alpha }_{b}$ correctly as it is to specify the correct function for inversion.

### Optional Input Parameters

1:     $\mathrm{n}$int64int32nag_int scalar
Default: the dimension of the array t.
$n$, the number of points at which the value of the inverse Laplace transform is required.
Constraint: ${\mathbf{n}}\ge 1$.
2:     $\mathrm{tfac}$ – double scalar
Default: $0.8$
${t}_{\mathrm{fac}}$, a factor to be used in calculating the parameter $\tau$. Larger values (e.g., $5.0$) may be specified for difficult problems, but these may require very large values of mxterm.
Constraint: ${\mathbf{tfac}}>0.5$.
3:     $\mathrm{mxterm}$int64int32nag_int scalar
Suggested value: ${\mathbf{mxterm}}\ge 100$, except for very simple problems.
Default: $100$
The maximum number of (complex) terms to be used in the evaluation of the Fourier series.
Constraint: ${\mathbf{mxterm}}\ge 1$.

### Output Parameters

1:     $\mathrm{valinv}\left({\mathbf{n}}\right)$ – double array
An estimate of the value of the inverse Laplace transform at $t={\mathbf{t}}\left(\mathit{j}\right)$, for $\mathit{j}=1,2,\dots ,n$.
2:     $\mathrm{errest}\left({\mathbf{n}}\right)$ – double array
An estimate of the error in ${\mathbf{valinv}}\left(j\right)$. This is usually an estimate of relative error but, if ${\mathbf{valinv}}\left(j\right)<{\mathbf{relerr}}$, ${\mathbf{errest}}\left(j\right)$ estimates the absolute error. ${\mathbf{errest}}\left(j\right)$ is unreliable when ${\mathbf{valinv}}\left(j\right)$ is small but slightly greater than relerr.
3:     $\mathrm{nterms}$int64int32nag_int scalar
The number of (complex) terms actually used.
4:     $\mathrm{na}$int64int32nag_int scalar
The number of values of $a$ used by the function. See Further Comments.
5:     $\mathrm{alow}$ – double scalar
The smallest value of $a$ used in the algorithm. This may be used for checking the value of ${\mathbf{alphab}}-$ see Further Comments.
6:     $\mathrm{ahigh}$ – double scalar
The largest value of $a$ used in the algorithm. This may be used for checking the value of ${\mathbf{alphab}}-$ see Further Comments.
7:     $\mathrm{nfeval}$int64int32nag_int scalar
The number of calls to fun made by the function.
8:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Note: nag_sum_invlaplace_crump (c06la) may return useful information for one or more of the following detected errors or 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.

${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}<1$, or ${\mathbf{mxterm}}<1$, or ${\mathbf{relerr}}<0.0$, or ${\mathbf{relerr}}\ge 1.0$, or ${\mathbf{tfac}}\le 0.5$.
${\mathbf{ifail}}=2$
 On entry, ${\mathbf{t}}\left(1\right)<0.0$, or ${\mathbf{t}}\left(1\right),{\mathbf{t}}\left(2\right),\dots ,{\mathbf{t}}\left({\mathbf{n}}\right)$ are not in strictly increasing order.
${\mathbf{ifail}}=3$
${\mathbf{t}}\left({\mathbf{n}}\right)$ is too large for this value of alphab. If necessary, scale the problem as described in Further Comments.
${\mathbf{ifail}}=4$
The required accuracy cannot be obtained. It is possible that alphab is less than $\alpha$. Alternatively, the problem may be especially difficult. Try increasing tfac, alphab or both.
${\mathbf{ifail}}=5$
Convergence failure in the epsilon algorithm. Some values of ${\mathbf{valinv}}\left(j\right)$ may be calculated to the desired accuracy; this may be determined by examining the values of ${\mathbf{errest}}\left(j\right)$. Try reducing the range of t or increasing mxterm. If ${\mathbf{ifail}}={\mathbf{5}}$ still results, try reducing tfac.
W  ${\mathbf{ifail}}=6$
All values of ${\mathbf{valinv}}\left(j\right)$ have been calculated but not all are to the requested accuracy; the values of ${\mathbf{errest}}\left(j\right)$ should be examined carefully. Try reducing the range of $t$, or increasing tfac, alphab or both.
${\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.

## Accuracy

The error estimates are often very close to the true error but, because the error control depends on an asymptotic formula, the required error may not always be met. There are two principal causes of this: Gibbs' phenomena, and zero or small values of the inverse Laplace transform.
Gibbs' phenomena (see the C06 Chapter Introduction) are exhibited near $t=0.0$ (due to the method) and around discontinuities in the inverse Laplace transform $f\left(t\right)$. If there is a discontinuity at $t=c$ then the method converges such that $f\left(c\right)\to \left(f\left(c-\right)+f\left(c+\right)\right)/2$.
Apparent loss of accuracy, when $f\left(t\right)$ is small, may not be serious. Crump's method keeps control of relative error so that good approximations to small function values may appear to be very inaccurate. If $\left|f\left(t\right)\right|$ is estimated to be less than relerr then this function switches to absolute error estimation. However, when $\left|f\left(t\right)\right|$ is slightly larger than relerr the relative error estimates are likely to cause ${\mathbf{ifail}}={\mathbf{6}}$. If this is found inconvenient it can sometimes be avoided by adding $k/p$ to the function $F\left(p\right)$, which shifts the inverse to $k+f\left(t\right)$.
Loss of accuracy may also occur for highly oscillatory functions.
More serious loss of accuracy can occur if $\alpha$ is unknown and is incorrectly estimated. See Further Comments.

### Timing

The value of $n$ is less important in general than the value of nterms. Unless fun is very inexpensive to compute, the timing is proportional to ${\mathbf{na}}×{\mathbf{nterms}}$. For simple problems ${\mathbf{na}}=2$ but in difficult problems na may be somewhat larger.

### Precautions

You are referred to the C06 Chapter Introduction for advice on simplifying problems with particular difficulties, e.g., where the inverse is known to be a step function.
The method does not work well for large values of $t$ when $\alpha$ is positive. It is advisable, especially if ${\mathbf{ifail}}={\mathbf{3}}$ is obtained, to scale the problem if $\left|\alpha \right|$ is much greater than $1.0$. See the C06 Chapter Introduction.
The range of values of $t$ specified for a particular call should not be greater than about $10$ units. This is because the method uses arguments based on the value ${\mathbf{t}}\left(n\right)$ and these tend to be less appropriate as $t$ becomes smaller. However, as the timing of the function is not especially dependent on $n$, it is usually far more efficient to evaluate the inverse for ranges of $t$ than to make separate calls to the function for each value of $t$.
The most important argument to specify correctly is alphab, an upper bound for $\alpha$. If, on entry, alphab is sufficiently smaller than $\alpha$ then completely incorrect results will be obtained with ${\mathbf{ifail}}={\mathbf{0}}$. Unless $\alpha$ is known theoretically it is strongly advised that you should test any estimated value used. This may be done by specifying a single value of $t$ (i.e ${\mathbf{t}}\left(n\right)$, $n=1$) with two sets of suitable values of tfac, relerr and mxterm, and examining the resulting values of alow and ahigh. The value of ${\mathbf{t}}\left(1\right)$ should be chosen very carefully and the following points should be borne in mind:
(i) ${\mathbf{t}}\left(1\right)$ should be small but not too close to $0.0$ because of Gibbs' phenomenon (see Accuracy),
(ii) the larger the value of ${\mathbf{t}}\left(1\right)$, the smaller the range of values of $a$ that will be used in the algorithm,
(iii) ${\mathbf{t}}\left(1\right)$ should ideally not be chosen such that $f\left({\mathbf{t}}\left(1\right)\right)=0.0$ or a very small value. For suitable problems ${\mathbf{t}}\left(1\right)$ might be chosen as, say, $0.1$ or $1.0$ depending on these factors. The function calculates alow from the formula
 $alow = alphab - ln0.1×relerr 2×τ .$
Additional values of $a$ are computed by adding $1/\tau$ to the previous value. As $\tau ={\mathbf{tfac}}×{\mathbf{t}}\left(n\right)$, it will be seen that large values of tfac and relerr will test for $a$ close to alphab. Small values of tfac and relerr will test for $a$ large. If the result of both tests is ${\mathbf{ifail}}={\mathbf{0}}$, with comparable values for the inverse, then this gives some credibility to the chosen value of alphab. You should note that this test could be more computationally expensive than the calculation of the inverse itself. The example program (see Example) illustrates how such a test may be performed.

## Example

This example estimates the inverse Laplace transform of the function $F\left(p\right)=1/\left(p+1/2\right)$. The true inverse of $F\left(p\right)$ is $\mathrm{exp}\left(-t/2\right)$. Two preliminary calls to the function are made to verify that the chosen value of alphab is suitable. For these tests the single value ${\mathbf{t}}\left(1\right)=1.0$ is used. To test values of $a$ close to alphab, the values ${\mathbf{tfac}}=5.0$ and ${\mathbf{relerr}}=0.01$ are chosen. To test larger $a$, the values ${\mathbf{tfac}}=0.8$ and ${\mathbf{relerr}}=\text{1.0e−3}$ are used. Because the values of the computed inverse are similar and ${\mathbf{ifail}}={\mathbf{0}}$ in each case, these tests show that there is unlikely to be a singularity of $F\left(p\right)$ in the region $-0.04\le \mathrm{Re}\left(p\right)\le 6.51$.
```function c06la_example

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

% Initialize variables and arrays.
mxterm = int64(200);
alphab = -0.5;
n = 1;
trures = zeros(1, n);
trurel = zeros(1, n);

% First, make two preliminary calls to the routine to verify that the
% chosen alphab value is suitable.  Use t = 1 for these calls.
t(1) = 1;
disp(['Test with t(1) = ',num2str(t(1))]);

% Parameter values for a close to alphab.
tfac = 7.5;
relerr = 1e-2;
fprintf(['\nmxterm = %3.0f   tfac = %3.2f   alphab = %3.2f ', ...
'relerr = %3.0e \n\n'], mxterm,tfac,alphab,relerr);

[valinv, errest, nterms, na, alow, ahigh, nfeval, ifail] = ...
c06la( ...
@fun, t, relerr, alphab, 'tfac', tfac, 'mxterm',mxterm);

% Calculate results and output them.
trures(1) = exp(double(-t(1)/2));
trurel(1) = abs((valinv(1)-trures(1))/trures(1));
disp('t     Result     exp(t/2)   Relative error   Error estimate')
fprintf('%1.1f   %8.4f    %8.4f      %8.4f         %8.4f\n',...
t(1), valinv(1), trures(1), trurel(1), errest(1));
fprintf(['\nnterms = %3d   nfeval = %3d   alow = %3.2f ', ...
'ahigh = %3.2f   ifail = %3d\n\n'], nterms, nfeval, alow, ...
ahigh, ifail);

% Parameter values for larger a.
tfac = 0.8;
relerr = 1e-3;
disp(['Test with t(1) = ',num2str(t(1))]);
fprintf(['\nmxterm = %3d   tfac = %3.2f   alphab = %3.2f ', ...
'relerr = %3.0e \n\n'], mxterm, tfac, alphab, relerr);

[valinv, errest, nterms, na, alow, ahigh, nfeval, ifail] = ...
c06la( ...
@fun, t, relerr, alphab, 'tfac', tfac, 'mxterm',mxterm);

% Calculate results and output them.
trures(1) = exp(double(-t(1)/2));
trurel(1) = abs((valinv(1)-trures(1))/trures(1));
disp('t     Result     exp(t/2)   Relative error   Error estimate')
fprintf('%1.1f   %8.4f    %8.4f      %8.5f         %8.5f\n',...
t(1), valinv(1), trures(1), trurel(1), errest(1));
fprintf(['\nnterms = %3d   nfeval = %3d   alow = %3.2f ', ...
'ahigh = %3.2f   ifail = %3d\n\n'], nterms, nfeval, alow, ...
ahigh, ifail);

% Now calculate the inverse Laplace transform for several t values.
n = 5;
t = [1:5];
disp('Compute inverse')
fprintf(['\nmxterm = %3d   tfac = %3.2f   alphab = %3.2f ', ...
'relerr = %3.0e \n\n'], mxterm, tfac, alphab, relerr);

[valinv, errest, nterms, na, alow, ahigh, nfeval, ifail] = ...
c06la( ...
@fun, t, relerr, alphab, 'tfac', tfac, 'mxterm',mxterm);

% Calculate results and output them.
disp('t    Result    exp(t/2)   Relative error  Error estimate')
for i = 1:n
trures(i) = exp(-t(i)/2);
trurel(i) = abs((valinv(i)-trures(i))/trures(i));
fprintf('%1.0f   ',t(i));
fprintf(' %4.3f      %4.3f      %4.3e      %4.3e\n',...
valinv(i),trures(i),trurel(i),errest(i));
end
fprintf(['\nmxterm = %3d   tfac = %3.2f   alphab = %3.2f ', ...
'relerr = %3.0e \n\n'], mxterm, tfac, alphab, relerr);

% Plot results.
fig1 = figure;
display_plot(t,trures,valinv,trurel,errest);

function [fr,fi] = fun(preal,pimag)
% Evaluate the real & imaginary parts of the user function.
z = complex(1)/complex(preal+0.5,pimag);
fr = real(z);
fi = imag(z);

function display_plot(t, trures, valinv, trurel, errest)
% Use a log plot for the second curve.
[haxes, hline1, hline2] = plotyy(t, valinv, t, trurel, 'plot', 'semilogy');
% Add the third curve (as points).
hold on
hpoints = plot(t, trures, '+', 'MarkerEdgeColor', 'r');
% Set the axis limits and the tick specifications to beautify the plot.
set(haxes(1), 'YLim', [0 0.8]);
set(haxes(1), 'YMinorTick', 'on');
set(haxes(1), 'YTick', [0.0 0.2 0.4 0.6 0.8]);
set(haxes(2), 'YLim', [1e-7 5e-3]);
set(haxes(2), 'YMinorTick', 'on');
set(haxes(2), 'YTick', [1e-7 1e-6 1e-5 1e-4]);
for iaxis = 1:2
% These properties must be the same for both sets of axes.
set(haxes(iaxis), 'XLim', [1 5]);
set(haxes(iaxis), 'XTick', [1 1.5 2 2.5 3 3.5 4 4.5 5]);
end
set(gca, 'box', 'off'); % no ticks on opposite axes.
title('Inverse Laplace Transform of 1/(p+0.5)');
% Label the x axis.
xlabel('t');
% Label the left and right y axes.
ylabel(haxes(1),'f(t)');
set(haxes(2),'YLim',[1e-8 Inf]);
ylabel(haxes(2),'Error');
% Add the fourth curve (plotted against the log axis).
axes(haxes(2));
hold on;
hline3 = semilogy(t, errest);
% Set some features of the three lines.
set(hline1, 'Linewidth', 0.5, 'MarkerFaceColor', 'auto');
set(hline2, 'Linewidth', 0.5, 'Marker', 'x', 'Color','Magenta');
set(hline3, 'Linewidth', 0.5, 'Marker', '*', 'MarkerFaceColor', 'auto');
legend([hpoints hline1 hline2 hline3], 'result', 'exp(-t/2)', ...
'relative error', 'estimated error', 'Location','SouthWest');
```
```c06la example results

Test with t(1) = 1

mxterm = 200   tfac = 7.50   alphab = -0.50 relerr = 1e-02

t     Result     exp(t/2)   Relative error   Error estimate
1.0     0.6071      0.6065        0.0010           0.0037

nterms =  18   nfeval =  36   alow = -0.04 ahigh = 0.09   ifail =   0

Test with t(1) = 1

mxterm = 200   tfac = 0.80   alphab = -0.50 relerr = 1e-03

t     Result     exp(t/2)   Relative error   Error estimate
1.0     0.6065      0.6065       0.00002          0.00008

nterms =  13   nfeval =  28   alow = 5.26 ahigh = 6.51   ifail =   0

Compute inverse

mxterm = 200   tfac = 0.80   alphab = -0.50 relerr = 1e-03

t    Result    exp(t/2)   Relative error  Error estimate
1    0.607      0.607      4.746e-05      3.155e-04
2    0.368      0.368      6.910e-06      9.386e-05
3    0.223      0.223      1.589e-05      7.839e-05
4    0.135      0.135      1.353e-05      7.504e-05
5    0.082      0.082      2.014e-05      8.080e-05

mxterm = 200   tfac = 0.80   alphab = -0.50 relerr = 1e-03

```