# Natural interpolation: spline

### 5.27.31 Natural interpolation: spline

The spline command finds the natural spline.

- spline takes four arguments:
  - Lx, a list of abscissas (in increasing order).
  - Ly, a list of ordinates (the same length as Lx).
  - x, a variable name.
  - l, an integer for the degree.
- spline(Lx,Ly,x,l) returns the natural spline function s of degree l, where s(Lx,j)=Ly,j for j=0..length(Lx), as a list of polynomials, each polynomial being valid on the corresponding interval determined by Lx.

#### Examples.

- Find the natural spline of degree 3, crossing through the points x0=0,y0=1, x1=1,y1=3 and x2=2, y2=0.
  Input:
  spline([0,1,2],[1,3,0],x,3)
  Output:

  ⎡
  ⎢
  ⎢
  ⎣

  −

  5
  4

  x3+

  13
  4

  x+1,

  5
  4

  ⎛
  ⎝

  x−1

  ⎞
  ⎠

  3−

  15
  4

  ⎛
  ⎝

  x−1

  ⎞
  ⎠

  2−

  x−1
  2

  +3

  ⎤
  ⎥
  ⎥
  ⎦

  Where the first polynomial, −5/4 x3+13/4 x+1, is defined on the interval [0,1] (the first interval defined by the list [0,1,2]) and the second polynomial 5/4 (x−1)3−15/4 (x−1)2−x−1/2+3 is defined on the interval [1,2], the second interval defined by the list [0,1,2].
- Find the natural spline of degree 4, crossing through the points x0=0,y0=1, x1=1,y1=3, x2=2, y2=0 and x3=3, y3=−1.
  Input:
  spline([0,1,2,3],[1,3,0,-1],x,4)
  Output:

  ⎡ ⎢ ⎢ ⎢ ⎣ | −

  62
  121

  x4+

  304
  121

  x+1,

  201
  121

  ⎛
  ⎝

  x−1

  ⎞
  ⎠

  4−

  248
  121

  ⎛
  ⎝

  x−1

  ⎞
  ⎠

  3−

  372
  121

  ⎛
  ⎝

  x−1

  ⎞
  ⎠

  2+

  56
  121

  ⎛
  ⎝

  x−1

  ⎞
  ⎠

  +3,

  −

  139
  121

  ⎛
  ⎝

  x−2

  ⎞
  ⎠

  4+

  556
  121

  ⎛
  ⎝

  x−2

  ⎞
  ⎠

  3+

  90
  121

  ⎛
  ⎝

  x−2

  ⎞
  ⎠

  2−

  628
  121

  ⎛
  ⎝

  x−2

  ⎞
  ⎠

  ⎤
  ⎥
  ⎥
  ⎥
  ⎦

  Output is a list of three polynomial functions of x, defined respectively on the intervals [0,1], [1,2] and [2,3].
- Find the natural spline interpolation of cos on [0,π/2,3π/2].
  Input:
  spline([0,pi/2,3*pi/2],cos([0,pi/2,3*pi/2]),x,3)
  Output:

  ⎡
  ⎢
  ⎢
  ⎢
  ⎣

  4 x3
  3 π 3

  −

  7 x
  3 π

  +1,

  −

  2

  ⎛
  ⎜
  ⎜
  ⎝

  x−

  π
  2

  ⎞
  ⎟
  ⎟
  ⎠

  3

  3 π 3

  +

  2

  ⎛
  ⎜
  ⎜
  ⎝

  x−

  π
  2

  ⎞
  ⎟
  ⎟
  ⎠

  2

  π 2

  −

  4

  ⎛
  ⎜
  ⎜
  ⎝

  x−

  π
  2

  ⎞
  ⎟
  ⎟
  ⎠

  3 π

  ⎤
  ⎥
  ⎥
  ⎥
  ⎦