Documentation |
Interpolation by cubic splines
This functionality does not run in MATLAB.
numeric::cubicSpline([x_{0}, y_{0}], [x_{1}, y_{1}], …, <BoundaryCondition>, <Symbolic>, <NoWarning>) numeric::cubicSpline([x_{0}, x_{1}, …], [y_{0}, y_{1}, …], <BoundaryCondition>, <Symbolic>, <NoWarning>) numeric::cubicSpline([[x_{0}, x_{1}, …], [y_{0}, y_{1}, …]], <BoundaryCondition>, <Symbolic>, <NoWarning>)
numeric::cubicSpline([x_{0}, y_{0}], [x_{1}, y_{1}], …) returns the cubic spline function interpolating a sequence of coordinate pairs [x_{i}, y_{i}].
The call S := numeric::cubicSpline([x_{0}, y_{0}], …, [x_{n}, y_{n}]) yields the cubic spline function S interpolating the data [x_{0}, y_{0}], …, [x_{n}, y_{n}], i.e., S(x_{i}) = y_{i} 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 x_{1}, …, x_{n - 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 x_{1} and x_{n - 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 x_{i} 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 x_{i} ≤ z < x_{i + 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 x_{i} ≤ z < x_{i + 1}.
If S is generated with symbolic abscissae x_{i} (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!
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 x_{0} = 0 ≤ z < x_{1} = 1:
expand(S1(z, 0)); expand(S2(z, 0)); expand(S3(z, 0)); expand(S4(z, 0))
delete data, S1, S2, S3, S4:
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 x_{0} = 0 ≤ z < x_{1} = 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:
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 x_{0} ≤ z < x_{1}:
S(z, 0)
delete S:
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:
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 t_{0}, …, t_{n} 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 [t_{0}, x_{0}], …, [t_{n}, x_{n}]. A spline interpoland Sy(t) approximating is obtained by interpolating the data [t_{0}, y_{0}], …, [t_{n}, y_{n}]:
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:
Symbolic |
With this option, no conversion of the input data to floating point numbers occurs. Symbolic abscissae x_{i} are accepted. The ordering x_{0} < x_{1} < … < x_{n} 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 x_{1} and x_{n - 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(x_{0}) = S(x_{n}), , . With this option, the input data y_{0}, y_{n} 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. |