MathMath

Help › Functions

Interpolation and fitting

linterp lspline pspline cspline bspline interp

linterp(vx, vy, x) As in Mathcad

The value at x of the straight-line segments joining the points (vx, vy).

vx must be in increasing order, and vx and vy the same length, at least two. x can be a number or a vector of numbers. Beyond either end, the first or last segment is carried on in a straight line.

Unlike the spline functions, linterp gives the value directly: there is no separate step.

Units

x is in vx's units and the result in vy's.

Refused

  • vx and vy of different lengths.
  • Fewer than two points.
  • x in units that don't match vx.
x≔⎝⎜⎜⎛​0123​⎠⎟⎟⎞​
y≔⎝⎜⎜⎛​0149​⎠⎟⎟⎞​
linterp(x,y,1.5)=2.5

lspline(vx, vy) As in Mathcad

A spline through the points (vx, vy) that ends as a straight line, for use with interp.

The curve is made of cubic pieces that join smoothly. At each end the curve becomes linear (its second derivative is 0 there). Give the result to interp with the same x and y to read values off the curve.

vx and vy are column vectors of the same length. vx must be in increasing order. Given an n×2 matrix of grid coordinates for vx and an n×n matrix for vy, it makes a spline for a surface, and interp then takes a point with two coordinates. The result is a vector of coefficients, not something to read: it is meant for interp. For other end behaviour see pspline and cspline.

Refused

  • vx and vy of different lengths.
  • A vx or vy that is complex or isn't a column vector.
x≔⎝⎜⎜⎛​0123​⎠⎟⎟⎞​
y≔⎝⎜⎜⎛​0149​⎠⎟⎟⎞​
S≔lspline(x,y)
interp(S,x,y,2.5)=6.35

pspline(vx, vy) As in Mathcad

A spline through the points (vx, vy) that ends as a parabola, for use with interp.

The curve is made of cubic pieces that join smoothly. At each end the curve becomes a parabola (its second derivative is constant there), so a table sampled from a parabola is reproduced exactly. Give the result to interp with the same x and y to read values off the curve.

Arguments and the 2-D form are as for lspline. See also cspline.

Refused

  • vx and vy of different lengths.
  • A vx or vy that is complex or isn't a column vector.
x≔⎝⎜⎜⎛​0123​⎠⎟⎟⎞​
y≔⎝⎜⎜⎛​0149​⎠⎟⎟⎞​
S≔pspline(x,y)
interp(S,x,y,2.5)=6.25

cspline(vx, vy) As in Mathcad

A spline through the points (vx, vy) that ends as a cubic, for use with interp.

The curve is made of cubic pieces that join smoothly. At each end the curve continues as one cubic, so a table sampled from a cubic (or a parabola, or a line) is reproduced exactly. Give the result to interp with the same x and y to read values off the curve.

Arguments and the 2-D form are as for lspline. See also pspline.

Refused

  • vx and vy of different lengths.
  • A vx or vy that is complex or isn't a column vector.
x≔⎝⎜⎜⎛​0123​⎠⎟⎟⎞​
y≔⎝⎜⎜⎛​0149​⎠⎟⎟⎞​
S≔cspline(x,y)
interp(S,x,y,2.5)=6.25

bspline(vx, vy, u, n) As in Mathcad

A piecewise polynomial of degree n through the points (vx, vy), with its pieces joined at the break points u, for use with interp.

n is the degree: 1, 2 or 3. u holds the break points, in increasing order, spanning the data. It has n−1 fewer entries than vx, so for degree 1 it is vx itself, and for degree 2 it has one fewer. Give the result to interp with the same x and y to read values off the curve.

The result holds the degree code, each piece's coefficients and each piece's ends, in the form Mathcad stores them.

Refused

  • A degree other than 1, 2 or 3.
  • A u with the wrong number of entries.
  • Break points that don't increase.
  • vx and vy of different lengths.
x≔⎝⎜⎜⎛​0123​⎠⎟⎟⎞​
y≔⎝⎜⎜⎛​0149​⎠⎟⎟⎞​
S≔bspline(x,y,x,1)
interp(S,x,y,2.5)=6.5

interp(vs, vx, vy, x) As in Mathcad

The value at x of the spline vs, made from the points (vx, vy).

vs comes from lspline, pspline, cspline or bspline, and vx and vy must be the same two vectors given to it. x can be a number or a vector. For a 2-D spline, x is a point with two coordinates, as a two-element vector.

Beyond the ends of the data the end behaviour of the spline carries on. For straight segments between points use linterp.

Units

x is in vx's units and the result in vy's.

Refused

  • A vs that didn't come from one of the spline functions.
  • A vs that doesn't match vx and vy.
  • A 2-D point without two coordinates.
x≔⎝⎜⎜⎛​0123​⎠⎟⎟⎞​
y≔⎝⎜⎜⎛​0149​⎠⎟⎟⎞​
S≔cspline(x,y)
interp(S,x,y,1.5)=2.25