Previous Up Next

11.2.4  Numerical spline interpolation

The interp command interpolates a discretized function at a list of given points by using fast spline interpolation routines in GSL. You should use this command when you do not need the interpolated function itself but only its values at a sequence of points.

Supported spline types

The description of spline types below is quoted from GSL documentation.

cubic
Cubic spline with natural boundary conditions. The resulting curve is piecewise cubic on each interval, with matching first and second derivatives at the supplied data-points. The second derivative is chosen to be zero at the first point and last point.
periodic cubic
Cubic spline with periodic boundary conditions. The resulting curve is piecewise cubic on each interval, with matching first and second derivatives at the supplied data-points. The derivatives at the first and last points are also matched. Note that the last point in the data must have the same y-value as the first point, otherwise the resulting periodic interpolation will have a discontinuity at the boundary.
(periodic) akima
Non-rounded Akima spline with natural (periodic) boundary conditions. This method uses the non-rounded corner algorithm of Wodicka.
steffen
Steffen’s method guarantees the monotonicity of the interpolating function between the given data points. Therefore, minima and maxima can only occur exactly at the data points, and there can never be spurious oscillations between data points. The interpolated function is piecewise cubic in each interval. The resulting curve and its first derivative are guaranteed to be continuous, but the second derivative may be discontinuous.

Example

This example is adapted from GSL documentation. To define some synthetic data, enter:

x,y:=makelist(k->k+0.5*sin(k),0,10),makelist(k->k+cos(k^2),0,10):;

This gives you 11 data points. To interpolate the function at intermediate points, enter:

t:=linspace(min(x),max(x),1000):; z:=interp(x,y,t):;

Now plot the data and the interpolated values together:

listplot(tran([t,z])); scatterplot(tran([x,y]),display=blue+point_width_2)

To see the difference between the supported spline types, interpolate in the segment [4,6]. Enter:

t:=linspace(4,6):; c,a,s:=interp(x,y,t,"cubic"),interp(x,y,t,"akima"),interp(x,y,t,"steffen"):; listplot(tran([t,c]),color=blue+quadrant4,legend="cubic"); listplot(tran([t,a]),color=red,legend="akima"); listplot(tran([t,s]),color=magenta,legend="steffen");

Previous Up Next