# NAG Library Routine Document

## 1Purpose

e01bff evaluates a piecewise cubic Hermite interpolant at a set of points.

## 2Specification

Fortran Interface
 Subroutine e01bff ( n, x, f, d, m, px, pf,
 Integer, Intent (In) :: n, m Integer, Intent (Inout) :: ifail Real (Kind=nag_wp), Intent (In) :: x(n), f(n), d(n), px(m) Real (Kind=nag_wp), Intent (Out) :: pf(m)
#include nagmk26.h
 void e01bff_ (const Integer *n, const double x[], const double f[], const double d[], const Integer *m, const double px[], double pf[], Integer *ifail)

## 3Description

e01bff evaluates a piecewise cubic Hermite interpolant, as computed by e01bef, at the points ${\mathbf{px}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,m$. If any point lies outside the interval from ${\mathbf{x}}\left(1\right)$ to ${\mathbf{x}}\left({\mathbf{n}}\right)$, a value is extrapolated from the nearest extreme cubic, and a warning is returned.
The routine is derived from routine PCHFE in Fritsch (1982).

## 4References

Fritsch F N (1982) PCHIP final specifications Report UCID-30194 Lawrence Livermore National Laboratory

## 5Arguments

1:     $\mathbf{n}$ – IntegerInput
2:     $\mathbf{x}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
3:     $\mathbf{f}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
4:     $\mathbf{d}\left({\mathbf{n}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: n, x, f and d must be unchanged from the previous call of e01bef.
5:     $\mathbf{m}$ – IntegerInput
On entry: $m$, the number of points at which the interpolant is to be evaluated. If any point lies outside the interval from ${\mathbf{x}}\left(1\right)$) to ${\mathbf{x}}\left({\mathbf{n}}\right)$, a value is extrapolated from the nearest extreme cubic, and a warning is returned. The extrapolation simply extends the final cubic at each end.
Constraint: ${\mathbf{m}}\ge 1$.
6:     $\mathbf{px}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayInput
On entry: the $m$ values of $x$ at which the interpolant is to be evaluated.
7:     $\mathbf{pf}\left({\mathbf{m}}\right)$ – Real (Kind=nag_wp) arrayOutput
On exit: ${\mathbf{pf}}\left(\mathit{i}\right)$ contains the value of the interpolant evaluated at the point ${\mathbf{px}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,m$.
8:     $\mathbf{ifail}$ – IntegerInput/Output
On entry: ifail must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this argument you should refer to Section 3.4 in How to Use the NAG Library and its Documentation for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this argument, the recommended value is $0$. When the value $-\mathbf{1}\text{​ 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 $-1$, explanatory error messages are output on the current error message unit (as defined by x04aaf).
Errors or warnings detected by the routine:
${\mathbf{ifail}}=1$
 On entry, ${\mathbf{n}}<2$.
${\mathbf{ifail}}=2$
The values of ${\mathbf{x}}\left(\mathit{r}\right)$, for $\mathit{r}=1,2,\dots ,{\mathbf{n}}$, are not in strictly increasing order.
${\mathbf{ifail}}=3$
 On entry, ${\mathbf{m}}<1$.
${\mathbf{ifail}}=4$
At least one of the points ${\mathbf{px}}\left(\mathit{i}\right)$, for $\mathit{i}=1,2,\dots ,{\mathbf{m}}$, lies outside the interval [${\mathbf{x}}\left(1\right),{\mathbf{x}}\left({\mathbf{n}}\right)$], and extrapolation was performed at all such points. Values computed at such points may be very unreliable.
${\mathbf{ifail}}=-99$
See Section 3.9 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
See Section 3.8 in How to Use the NAG Library and its Documentation for further information.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
See Section 3.7 in How to Use the NAG Library and its Documentation for further information.

## 7Accuracy

The computational errors in the array pf should be negligible in most practical situations.

## 8Parallelism and Performance

e01bff is not threaded in any implementation.

The time taken by e01bff is approximately proportional to the number of evaluation points, $m$. The evaluation will be most efficient if the elements of px are in nondecreasing order (or, more generally, if they are grouped in increasing order of the intervals $\left[{\mathbf{x}}\left(r-1\right),{\mathbf{x}}\left(r\right)\right]$). A single call of e01bff with $m>1$ is more efficient than several calls with $m=1$.
As documented above, this routine will use extrapolation if presented with evaluation points outside the region $\left[{x}_{1},{x}_{n}\right]$. Since such extrapolated values are computed simply by extending the cubic approximation at each end interval, the values may not be suitable for all purposes. If you need more control over how values outside the original region are calculated, consider the following possible procedures for degree $0$, $1$ and $2$ extrapolation.
 (i) Flat extrapolation For $x<{x}_{1}$ choose $f={\mathbf{f}}\left(1\right)$; for $x>{x}_{n}$ choose $f={\mathbf{f}}\left({\mathbf{n}}\right)$. (ii) Linear extrapolation For $x<{x}_{1}$, call e01bgf using ${\mathbf{px}}\left(1\right)={\mathbf{x}}\left(1\right)$ to obtain ${\mathbf{pd}}\left(1\right)$, then choose $f={\mathbf{pd}}\left(1\right)×\left(x-{\mathbf{x}}\left(1\right)\right)+{\mathbf{f}}\left(1\right)$; for $x>{x}_{n}$, call e01bgf using ${\mathbf{px}}\left(1\right)={\mathbf{x}}\left({\mathbf{n}}\right)$ to obtain ${\mathbf{pd}}\left({\mathbf{n}}\right)$, then choose $f={\mathbf{pd}}\left({\mathbf{n}}\right)×\left(x-{\mathbf{x}}\left({\mathbf{n}}\right)\right)+{\mathbf{f}}\left({\mathbf{n}}\right)$. (iii) Quadratic extrapolation For $x<{x}_{1}$, call e01bgf to obtain derivative values ${\mathbf{pd}}\left(1\right)$ and ${\mathbf{pd}}\left(2\right)$ at ${x}_{1}$ and ${x}_{2}$, if $\left|{\mathbf{pd}}\left(2\right)\right|\ge \left|{\mathbf{pd}}\left(1\right)\right|$ revert to linear extrapolation (ii),otherwise let ${l}_{1}\left(x\right)=\left(x-{x}_{1}\right),{l}_{2}\left(x\right)=\left(x-{x}_{2}\right)$ and $c=1/\left({x}_{1}-{x}_{2}\right)$, then choose $f\left(x\right)={\mathbf{pd}}\left(1\right)×{l}_{1}\left(x\right)×{l}_{2}\left(x\right)×c-{\mathbf{f}}\left(1\right)×{l}_{2}\left(x\right)×\left({l}_{1}\left(x\right)+{l}_{1}\left({x}_{2}\right)\right)×{c}^{2}+{\mathbf{f}}\left(2\right)×{\left[{l}_{1}\left(x\right)×c\right]}^{2}$; for $x>{x}_{n}$, call e01bgf to obtain derivative values ${\mathbf{pd}}\left({\mathbf{n}}\right)$ and ${\mathbf{pd}}\left({\mathbf{n}}-1\right)$ at ${x}_{n}$ and ${x}_{n-1}$, if $\left|{\mathbf{pd}}\left({\mathbf{n}}-1\right)\right|\ge \left|{\mathbf{pd}}\left({\mathbf{n}}\right)\right|$ revert to linear extrapolation (ii),otherwise let ${l}_{n}\left(x\right)=\left(x-{x}_{n}\right),{l}_{n-1}\left(x\right)=\left(x-{x}_{n-1}\right)$ and $c=1/\left({x}_{n}-{x}_{n-1}\right)$, then choose $f\left(x\right)={\mathbf{pd}}\left({\mathbf{n}}\right)×{l}_{n}\left(x\right)×{l}_{n-1}\left(x\right)×c-{\mathbf{f}}\left({\mathbf{n}}\right)×{l}_{n-1}\left(x\right)×\left({l}_{n}\left(x\right)+{l}_{n}\left({x}_{n-1}\right)\right)×{c}^{2}+{\mathbf{f}}\left({\mathbf{n}}-1\right)×{\left[{l}_{n}\left(x\right)×c\right]}^{2}$.

## 10Example

This example reads in values of n, x, f and d, and then calls e01bff to evaluate the interpolant at equally spaced points.

### 10.1Program Text

Program Text (e01bffe.f90)

### 10.2Program Data

Program Data (e01bffe.d)

### 10.3Program Results

Program Results (e01bffe.r)

© The Numerical Algorithms Group Ltd, Oxford, UK. 2017