# Documentation

### This is machine translation

Translated by
Mouseover text to see original. Click the button below to return to the English verison of the page.

# `numeric`::`cubicSpline`

Interpolation by cubic splines

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.

## Syntax

```numeric::cubicSpline(`[x0, y0], [x1, y1], …`, <`BoundaryCondition`>, <Symbolic>, <NoWarning>)
numeric::cubicSpline(`[x0, x1, …]`, `[y0, y1, …]`, <`BoundaryCondition`>, <Symbolic>, <NoWarning>)
numeric::cubicSpline(`[[x0, x1, …], [y0, y1, …]]`, <`BoundaryCondition`>, <Symbolic>, <NoWarning>)
```

## Description

```numeric::cubicSpline([x0, y0], [x1, y1], …)``` returns the cubic spline function interpolating a sequence of coordinate pairs [xi, yi].

The call ```S := numeric::cubicSpline([x0, y0], …, [xn, yn])``` yields the cubic spline function S interpolating the data [x0, y0], …, [xn, yn], i.e., S(xi) = yi for i = 0, …, n. The spline function is a piecewise polynomial of degree ≤ 3 on the intervals . S and its first two derivatives are continuous at the points x1, …, xn - 1. Note that S extends the polynomial representation on , to and , respectively.

By default, `NotAKnot` boundary conditions are assumed, i.e., the third derivative is continuous at the points x1 and xn - 1. With this boundary condition, S is a cubic polynomial on the intervals and .

By default, all input data are converted to floating-point numbers. This conversion may be suppressed by the option `Symbolic`.

Without the option `Symbolic`, the abscissae xi must be numerical real values in ascending order. If these data are not ordered, then `numeric::cubicSpline` reorders the abscissae internally, issuing a warning. The warning may be switched off by the option `NoWarning`.

The function S returned by `numeric::cubicSpline` may be called with one, two or three arguments:

• The call `S(z)` returns an explicit expression or a number, if `z` is a real number. Otherwise, the symbolic call `S(z)` is returned.

• The call `S(z, [k])` with a nonnegative integer `k` returns the k-th derivative of S. Cf. Example 4. For k > 3, zero is returned for any z.

• The call `S(z, i)` is meant for symbolic arguments z. The argument i must be an integer. Internally, z is assumed to satisfy xiz < xi + 1, and `S(z, i)` returns the polynomial expression in z representing the spline function on this interval.

• The call `S(z, i, [k])` with an integer i and a nonnegative integer k returns the polynomial representation of the k-th derivative of the spline function on the interval xiz < xi + 1.

If `S` is generated with symbolic abscissae xi (necessarily using the option `Symbolic`), then the call `S(z)` with numerical `z` leads to an error. The call ```S(z, i)``` must be used for symbolic abscissae!

### Note

Note that the interpolation of 2 points (x0, y0), (x1, y1) must be specified by ```numeric::cubicSpline( [x0, y0] , [x1, y1] )```, not by ```numeric::cubicSpline( [x0, x1] , [y0, y1] )```!

## Examples

### Example 1

We demonstrate some calls with numerical input data:

```data := [i, sin(i*PI/20)] \$ i= 0..40: S1 := numeric::cubicSpline(data): S2 := numeric::cubicSpline(data, Natural): S3 := numeric::cubicSpline(data, Periodic): S4 := numeric::cubicSpline(data, Complete = [3, PI]):```

At the abscissae, the corresponding input data are reproduced:

`float(data[6][2]), S1(5), S2(5), S3(5), S4(5)`

Interpolation between the abscissae depends on the boundary condition:

`S1(4.5), S2(4.5), S3(4.5), S4(4.5)`

These are the cubic polynomials in z defining the spline on the interval x0 = 0 ≤ z < x1 = 1:

```expand(S1(z, 0)); expand(S2(z, 0)); expand(S3(z, 0)); expand(S4(z, 0))```

`delete data, S1, S2, S3, S4:`

### Example 2

We demonstrate some calls with symbolic data:

`S := numeric::cubicSpline([i, y.i] \$ i = 0..3):`
`S(1/2)`

This is the cubic polynomial in z defining the spline on the interval x0 = 0 ≤ z < x1 = 1:

`S(z, 0)`

With the option `Symbolic`, exact arithmetic is used:

`S := numeric::cubicSpline([i, y.i] \$ i = 0..3, Symbolic):`
`S(1/2)`

Also symbolic boundary data are accepted:

```S := numeric::cubicSpline([i, exp(i)] \$ i = 0..10, Complete = [a, b]):```
`S(0.1)`

```S := numeric::cubicSpline([0, y0], [1, y1], [2, y2], Symbolic, Complete = [a, 5]):```
`collect(S(z, 0), z)`

`delete S:`

### Example 3

We demonstrate the use of symbolic abscissae. Here the option `Symbolic` is mandatory.

`S := numeric::cubicSpline([x.i, y.i] \$ i = 0..2, Symbolic):`

The spline function `S` can only be called with 2 arguments. This is the cubic polynomial in `z` defining the spline on the interval x0z < x1:

`S(z, 0)`

`delete S:`

### Example 4

We plot a spline function together with its first three derivatives. The spline approximates the function sin(x):

```n := 10: x := array(0..n, [i/n*2*PI \$ i = 0..n]): S := numeric::cubicSpline([x[i], sin(x[i])] \$ i = 0..n, Natural): delete x: plot( plot::Function2d(S(x), x = 0..2*PI, Color = RGB::Black), plot::Function2d(S(x, [1]), x = 0..2*PI, Color = RGB::Red), plot::Function2d(S(x, [2]), x = 0..2*PI, Color = RGB::Green, Mesh = 1000), plot::Function2d(S(x, [3]), x = 0..2*PI, Color = RGB::Blue, Mesh = 1000) )```

`delete n, S:`

### Example 5

We demonstrate how to generate a phase plot of the differential equation , with initial conditions . First, we use `numeric::odesolve` to compute a numerical mesh of solution points with n + 1 equidistant time nodes t0, …, tn in the interval [0, 20]:

```DIGITS := 4: n := 100: for i from 0 to n do t[i] := 20/n*i: end_for: f := (t, x) -> [x[2], sin(t) - x[1]^3]: x[0] := 0: y[0] := 0: for i from 1 to n do [x[i], y[i]] := numeric::odesolve(t[i-1]..t[i], f, [x[i-1], y[i-1]]): end_for:```

The mesh of the phase plot consists of the following points:

`Plotpoints := [[x[i], y[i]] \$ i = 0..n]:`

We wish to connect these points by a spline curve. We define a spline interpoland Sx(t) approximating the solution x(t) by interpolating the data [t0, x0], …, [tn, xn]. A spline interpoland Sy(t) approximating is obtained by interpolating the data [t0, y0], …, [tn, yn]:

```Sx := numeric::cubicSpline([t[i], x[i]] \$ i = 0..n): Sy := numeric::cubicSpline([t[i], y[i]] \$ i = 0..n):```

Finally, we plot the mesh points together with the interpolating spline curve:

```plot( plot::PointList2d(Plotpoints, PointColor = RGB::Black), plot::Curve2d([Sx(z), Sy(z)], z = 0..20, Mesh = 5*(n - 1) + 1, LineColor = RGB::Red) )```

The functions `plot::Ode2d` and `plot::Ode3d` serve for displaying numerical solutions of ODEs. In fact, they are implemented as indicated by the previous commands. The following call produces the same plot:

```plot(plot::Ode2d( [t[i] \$ i = 0..n], f, [x[0], y[0]], [(t, x) -> [x[1], x[2]], Style = Points, Color = RGB::Black], [(t, x) -> [x[1], x[2]], Style = Splines, Color = RGB::Red])):```

`delete DIGITS, n, i, t, f, x, y, Plotpoints, Sx, Sy:`

## Parameters

 `x0, x1, …` Numerical real values in ascending order `y0, y1, …` Arbitrary expressions `BoundaryCondition` The type of the boundary condition: either `NotAKnot`, `Natural`, `Periodic`, or `Complete = [a, b]` with arbitrary arithmetical expressions `a, b`.

## Options

 `Symbolic` With this option, no conversion of the input data to floating point numbers occurs. Symbolic abscissae xi are accepted. The ordering x0 < x1 < … < xn is assumed by `numeric::cubicSpline`. This ordering is not checked, even if the abscissae are numerical! `NoWarning` The x-values of the interpolation points must be in ascending order. If the input data violate this condition, the routine issues a warning and reorders the data automatically. With this option, the warning is switched off. `NotAKnot` With the default boundary condition `NotAKnot`, the third derivative of the spline function is continuous at the points x1 and xn - 1. With this boundary condition, S is a polynomial on the intervals and . `Natural` The boundary condition `Natural` produces a spline function S satisfying . `Periodic` The boundary condition `Periodic` produces a spline function S satisfying S(x0) = S(xn), , . With this option, the input data y0, yn must coincide, otherwise an error is raised. `Complete` Option, specified as `Complete = [a, b]` The boundary condition `Complete = [a, b]` produces a spline function S satisfying , . Symbolic data `a, b` are accepted.