cub_spline_value
Computes the value of a cubic spline or the value of one of its derivatives.
Synopsis
#include <imsl.h>
float imsl_f_cub_spline_value (float x, Imsl_f_ppoly *ppoly, …, 0)
The type double function is imsl_d_cub_spline_value.
Required Arguments
float x (Input)
Evaluation point for the cubic spline.
Imsl_f_ppoly *ppoly (Input)
Pointer to the piecewise polynomial structure that represents the cubic spline.
Return Value
The value of a cubic spline or one of its derivatives at the point x. If no value can be computed, then NaN is returned.
Synopsis with Optional Arguments
#include <imsl.h>
float imsl_f_cub_spline_value (float x, Imsl_f_ppoly *ppoly,
IMSL_DERIV, int deriv,
IMSL_GRID, int n, float *xvec, float **value,
IMSL_GRID_USER, int n, float *xvec, float value_user[],
0)
Optional Arguments
IMSL_DERIV, int deriv (Input)
Let d = deriv and let s be the cubic spline that is represented by the structure *ppoly, then this option produces the d-th derivative of s at x, s(d) (x).
IMSL_GRID, int n, float *xvec, float **value (Input/Output)
The array xvec of length n contains the points at which the cubic spline is to be evaluated. The d-th derivative of the spline at the points in xvec is returned in value. The values in array xvec must appear sorted and non-decreasing. Arranging for this requirement may benefit by use of the function imsl_f_sort, Chapter 12.
IMSL_GRID_USER, int n, float *xvec, float value_user[] (Input/Output)
The array xvec of length n contains the points at which the cubic spline is to be evaluated. The d-th derivative of the spline at the points in xvec is returned in the user-supplied space value_user. The values in array xvec must appear sorted and non-decreasing..
Description
The function imsl_f_cub_spline_value computes the value of a cubic spline or one of its derivatives. The first and last pieces of the cubic spline are extrapolated. As a result, the cubic spline structures returned by the cubic spline routines are defined and can be evaluated on the entire real line. This routine is based on the routine PPVALU by de Boor (1978, p. 89).
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).
#include <imsl.h>
#include <stdio.h>
#include <math.h>
#define NDATA 11
/* Define function */
#define F(x) (float)(sin(15.0*x))
int main()
{
int i;
float fdata[NDATA], xdata[NDATA], x, y;
Imsl_f_ppoly *pp;
/* Set up a grid */
for (i = 0; i < NDATA; i++) {
xdata[i] = (float)i /((float)(NDATA-1));
fdata[i] = F(xdata[i]);
}
/* Compute cubic spline interpolant */
pp = imsl_f_cub_spline_interp_e_cnd (NDATA, xdata, fdata, 0);
/* Print results */
printf(" x F(x) Interpolant Error\n");
for (i = NDATA/2; i < 3*NDATA/2; i++) {
x = (float) i /(float)(2*NDATA-2);
y = imsl_f_cub_spline_value(x, pp, 0);
printf(" %6.3f %10.3f %10.3f %10.4f\n", x, F(x), y,
fabs(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.
#include <imsl.h>
#include <stdio.h>
#include <math.h>
#define NDATA 11
/* Define functions */
#define F(x) (float)(sin(15.0*x))
#define FP(x) (float)(15.*cos(15.0*x))
int main()
{
int i;
float fdata[NDATA], xdata[NDATA], x, y;
Imsl_f_ppoly *pp;
/* Set up a grid */
for (i = 0; i < NDATA; i++) {
xdata[i] = (float)i /((float)(NDATA-1));
fdata[i] = F(xdata[i]);
}
/* Compute cubic spline interpolant */
pp = imsl_f_cub_spline_interp_e_cnd (NDATA, xdata,fdata, 0);
/* Print results */
printf(" x FP(x) Interpolant Deriv Error\n");
for (i = NDATA/2; i < 3*NDATA/2; i++){
x = (float) i /(float)(2*NDATA-2);
y = imsl_f_cub_spline_value(x, pp,
IMSL_DERIV, 1,
0);
printf(" %6.3f %10.3f %10.3f %10.4f\n", x, FP(x), y,
fabs(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