splineValue

Computes the value of a spline or the value of one of its derivatives.

Synopsis

splineValue (x, sp)

Required Arguments

float x (Input)
Evaluation point for the spline.
Imsl_d_spline sp (Input)
The structure that represents the spline.

Return Value

The value of a spline or one of its derivatives at the point x. If no value can be computed, NaN is returned.

Optional Arguments

deriv, int (Input)

Let d = deriv and let s be the spline that is represented by the structure sp. Then, this option produces the d-th derivative of s at x, \(s^{(d)}(x)\).

Default: deriv = 0

grid, float xvec, float value (Input/Output)
The argument xvec is the array of length n containing the points at which the spline is to be evaluated. The d-th derivative of the spline at the points in xvec is returned in value.

Description

The function splineValue computes the value of a spline or one of its derivatives. This function is based on the routine BVALUE by de Boor (1978, p. 144).

Examples

Example 1

In this example, a cubic spline interpolant to a function f is computed. The values of this spline are then compared with the exact function values. Since the default settings are used, the interpolant is determined by the “not-a-knot” condition (see de Boor 1978).

from __future__ import print_function
from numpy import *
from pyimsl.math.splineInterp import splineInterp
from pyimsl.math.splineValue import splineValue

# Define function


def F(x):
    return sin(15.0 * x)


# Set up a grid
ndata = 11
xdata = empty(ndata)
fdata = empty(ndata)
for i in range(0, ndata):
    xdata[i] = float(i) / (ndata - 1)
    fdata[i] = F(xdata[i])

# Compute cubic spline interpolant
sp = splineInterp(xdata, fdata)

# Print results
print("     x        F(x)     Interpolant     Error")
for i in range(int(ndata / 2), int(3 * ndata / 2)):
    x = float(i) / (2 * ndata - 2)
    y = splineValue(x, sp)
    print('%6.3f  %10.3f   %10.3f   %10.4f' %
          (x, F(x), y, abs(F(x) - y)))

Output

     x        F(x)     Interpolant     Error
 0.250      -0.572       -0.549       0.0228
 0.300      -0.978       -0.978       0.0000
 0.350      -0.859       -0.843       0.0162
 0.400      -0.279       -0.279       0.0000
 0.450       0.450        0.441       0.0093
 0.500       0.938        0.938       0.0000
 0.550       0.923        0.903       0.0199
 0.600       0.412        0.412       0.0000
 0.650      -0.320       -0.315       0.0049
 0.700      -0.880       -0.880       0.0000
 0.750      -0.968       -0.938       0.0295

Example 2

Recall that in the first example, a cubic spline interpolant to a function f is computed. The values of this spline are then compared with the exact function values. This example compares the values of the first derivatives.

from __future__ import print_function
from numpy import *
from pyimsl.math.splineInterp import splineInterp
from pyimsl.math.splineValue import splineValue

# Define functions


def F(x):
    return sin(15.0 * x)


def FP(x):
    return 15.0 * cos(15.0 * x)


# Set up a grid
ndata = 11
xdata = empty(ndata)
fdata = empty(ndata)
for i in range(0, ndata):
    xdata[i] = float(i) / (ndata - 1)
    fdata[i] = F(xdata[i])

# Compute cubic spline interpolant
sp = splineInterp(xdata, fdata)

# Print results
print("     x        FP(x)   Interpolant   Deriv Error")
for i in range(int(ndata / 2), int(3 * ndata / 2)):
    x = float(i) / (2 * ndata - 2)
    y = splineValue(x, sp, deriv=1)
    print('%6.3f  %10.3f   %10.3f   %10.4f' %
          (x, FP(x), y, abs(FP(x) - y)))

Output

     x        FP(x)   Interpolant   Deriv Error
 0.250     -12.308      -12.559       0.2510
 0.300      -3.162       -3.218       0.0560
 0.350       7.681        7.796       0.1151
 0.400      14.403       13.919       0.4833
 0.450      13.395       13.530       0.1346
 0.500       5.200        5.007       0.1926
 0.550      -5.786       -5.840       0.0535
 0.600     -13.667      -13.201       0.4660
 0.650     -14.214      -14.393       0.1798
 0.700      -7.133       -6.734       0.3990
 0.750       3.775        3.911       0.1359

Fatal Errors

IMSL_KNOT_MULTIPLICITY Multiplicity of the knots cannot exceed the order of the spline.
IMSL_KNOT_NOT_INCREASING The knots must be nondecreasing.