Interpolation by cubic splines

Directly to the input form

 
  • Here we have a code for computing cubic interpolating splines. These may serve for approximating a given function f by a two times differentiable function, composed from polynomials of degree not larger than three. While interpolation by polynomials gives useful results on small intervals only, this type of approximation is not sensitive against the total length of the interval but only to the distance of the abscissae.
  • The function obtained, we name it S here, is piecewise defined, and each such piece lives between two successive abscissae of the grid
    a = x1 < x2 < ... < xn = b
  • These pieces, named Si, i=1,...,n-1 here, are determined from the following conditions:
    1. Si(x) is a polynomial of degree not greater 3.
    2. S(x) = Si(x) for xi <= x <= xi+1
    3. Si(xi) = f(xi) (or yi).
    4. Si(xi+1) = f(xi+1).
    5. S'i(xi+1) = S'i+1(xi+1), i=1,..,n-2 (continuity of S').
    6. S''i(xi+1) = S''i+1(xi+1), i=1,...,n-2 (continuity of S'').
    Observe that the continuity of S itself is obtained by the interpolation condition.
  • These conditions lead to linear system of 4(n-2)+2 equations for the 4(n-1) coefficients which is broken down here in a special way into a tridiagonal linear system and n-1 uncoupled systems of dimension 3 with a triangular structure.
  • This construction is always possible, but not unique. There remain two degrees of freedom, which allow to fix properties of S at the boundaries of its interval of definition. Several choices are possible and in use, for example continuity of S''' at x2 and xn-1 (giving the ordinary cubic interpolation in case n=4) , periodicity of S' and S'', which of course is useful only if also f is periodic (i.e. y1=yn) and the two used here: the so called ''natural'' spline with S''(a) = S''(b) = 0 and the hermitian spline with S'(a) = f'(a) and S'(b) = f'(b). The natural spline got its name from the spline of the shipbuilder, a thin elastic ruler, and the hermitian from Hermites interpolation scheme, where values of f and f' are interpolated simultaneously.
  • The spline has the remarkable property of minimizing the total curvature integral
    ab (g''(x))2dx
    over the set of all two times continuously differentiable functions g which satisfy the interpolation and the boundary conditions specified.
  • The approximation error of those two constructions is of the order h4, with h = max(xi+1-xi), provided that f is four times continuously differentiable on [a,b]. This holds true for the hermitian spline on the whole interval and for the natural one in [x1+ε,xn-ε] with the error constant growing to infinity for ε->0. If in case of the hermitian spline one replaces the true values f'(a), f'(b) by the derivative of the cubic interpolation polynomial for the first resp. last four points of the grid at a resp. b, then the error constant raises a bit, but the overall quality is unchanged. Hence one can use this if no derivative is available explicitly.
 

Input

 
  • You can choose between input of a data set of your own or the artificial generation of such a data set using a predefined or self defined function.
  • If you specify your own data set then only its length and the data are specified and a choice between the natural or the hermitian spline is made. In the case of the latter you explicitly define the boundary slopes as numbers. you may want to see explicitly the piecewise definition of the spline.
  • In the other case you are in the situation of a numerical experiment concerning the approximation properties of these splines. you specify a function, the data set is computed using this and then the spline and its error curve is shown. There are 5 predefined functions and you can specify a function yourself.
    1. You may choose between the natural and the hermitian spline and in the case of the latter whether the boundary derivatives should be computed analytically or by classical cubic interpolation using four data points from the left resp. right end.
    2. In the case of specification of a function of your own you must follow FORTRAN conventions.
    3. If you want to compute the derivatives f'(a),f'(b) analytically the piece of code specifying the computation of f must follow the restrictions of JAKEF since we generate the code for f' using this.
    4. You specify the number n of abscissae, 4 <= n <= 200.
    5. You specify the interval [a,b]. The abscissae are taken equidistant from there.
 

Output

 
  • In case of data of your own you get a plot of the data and the interpolating spline, and if wanted, the formula representation of the spline.
  • In the other case, you get a protocol showing
    1. the choice of the spline, the number of data and the choice of the function.
    2. Two plots showing f(x) and S(x) resp. the error f(x)-S(x).
 

Questions?!

 
  • How developes the spline if you increase n?
  • How may you check the correctness of the statement concerning the order of convergence?
  • What takes place if f is nonsmooth?
  • Compare the spline and the polynomial interpolation on the same set of data!
 

To the input form

 
 
Back to the top!

18.02.2015