Topic : Additional Interpolation Topics
Simulation : The Need for Spline Interpolation
Language : Mathematica 4.1
Authors : Nathan Collier,Autar Kaw
Date : 12 December 2002
Abstract : This simulation shows the need for spline interpolation as opposed to using polynomial interpolation. The function chosen was first used by Runge in 1901 when he wanted to show that higher order interpolation is a bad idea.  He took a simple looking function f[x]=1/(1+25*x^2) and chose equidistantly spaced points to interpolate the function

Clear[x, y, f, M, f1, A, xy]

INPUTS: Enter the following

Enter the number of points chosen for interpolation in [-1,1].  For the number scheme to function properly, the number of points selected should be odd.

n := 9;

SOLUTION

f[x_] := 1/(1 + 25*x^2)

Runge = Plot[f[x], {x, -1, 1}];

When n is given, this returns an array containing sequential values of x.

x := Table[2/(n - 1)*i - 1, {i, 0, n - 1}]

y := f[x]

xy = Table[0, {i, 1, n}, {j, 1, 2}];
Do[xy[[i, 1]] = x[[i]]; 
        xy[[i, 2]] = y[[i]], {i, 1, n}];

When x and y data and order is given, this constructs the matrix whose inverse is needed to find the coefficients of the polynomial which approximates the data.

M = 
        Table[x[[i + 1]]^j, {i, 0, n - 1}, {j, 0, n - 1}];

M[[(n + 1)/2, 1]] = 1;

A = LinearSolve[M, y];

f1[z_] := 
      Sum[A[[i]]*z^(i - 1), {i, 1, n}]
\(	\)

poly = Plot[f1[z], {z, -1, 1}];
data = 
        ListPlot[xy, PlotStyle -> PointSize[0.02], 
          TextStyle -> {FontSize -> 11}, 
          PlotRange -> All];

<< Graphics`Spline`

splin = 
        Show[Graphics[{Spline[xy, Cubic]}], 
          PlotRange -> All];

Show[splin, poly, data,  Runge, Axes ->  True];