Numerical spline interpolation

### 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.

- interp takes two or three mandatory arguments and a sequence of optional arguments:
  - data, a sequence or list of two vectors x and y of the same length.
  - v, a vector of x-values at which the spline is evaluated.
  - Optionally, opts, a sequence of options each of which is one of:
    - "stype", a string specifying spline type. Allowed values for stype are cubic, akima and steffen (by default, stype=cubic).
    - periodic, the symbol specifying that interpolation should be periodic (by default, interpolation is non-periodic). This argument gets ignored if stype=steffen.
    - d, a nonnegative integer specifying the order of the derivative of interpolated function to be evaluated. Allowed values are 0, 1 and 2 (by default, d=0).
- interp([x,y],v ⟨,opts ⟩) or interp(x,y,v ⟨,opts ⟩) returns the list z of spline (derivative) values at points in v.
- Note that data and v must be entirely numeric and data should also be inexact.
- The elements of x must be strictly ascending. The minimal number of data points is 3.
- The elements of t must be between minx and maxx, otherwise the output list will contain undefined elements (as an effect, plotting would fail).

#### 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");