Application Center - Maplesoft

App Preview:

The Need for Spline Interpolation

You can switch back to the summary page by clicking here.

Learn about Maple
Download Application


 

needSplines.mws

The Need for Spline Interpolation.

2003 Nathan Collier, Autar Kaw, Jai Paul , University of South Florida , kaw@eng.usf.edu , http://numericalmethods.eng.usf.edu/mws .

This worksheet demonstrates the use of Maple to motivate spline interpolation. It illustrates how interpolation using splines can be more accurate when compared to interpolation using polynomials.

Introduction

The following example illustrates the need for spline interpolation as opposed to using polynomial interpolation.   In the year 1901, Runge tried to explain that higher order interpolation is a bad idea. He took a simple function [Maple OLE 2.0 Object]  and chose equidistantly spaced data points to interpolate this function. The same function is used in the following example and is interpolated using polynomial interpolation and cubic spline interpolation.

>    restart;

Warning, the protected names norm and trace have been redefined and unprotected

Section I : Data.

The points chosen in this example are 9 equidistant points in [-1,1]. They are [-1, -0.75, -0.5, -0.25, 0, 0.25, 0.5, 0.75, 1].

Plotting Runge's Function:

>    fRunge:=x->1/(1+25*x^2);

fRunge := proc (x) options operator, arrow; 1/(1+25*x^2) end proc

>    plot(fRunge,-1..1,-0.5..1,thickness=4,title="Runge's function");

[Maple Plot]

Section II: Polynomial Interpolation.

The function is interpolated using 9 equidistant data points in [-1,1] to obtain an 8th order polynomial.   By looking at the plot of the original function and the 8th order polynomial, you can see that the polynomial interpolation does not accurately represent the function.  One may think that choosing more points would help in alleviating this problem, but in fact it makes it worse.

>    poly_fn:=interp([-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1],[fRunge(-1),fRunge(-0.75),fRunge(-0.5),fRunge(-0.25),fRunge(0),fRunge(0.25),fRunge(0.5),fRunge(0.75),fRunge(1)],t);

poly_fn := 53.68930043*t^8-.13e-6*t^7+1.000000000-102.8150104*t^6+.16e-8*t+.19e-6*t^5-13.20303455*t^2+61.36720611*t^4-.3e-7*t^3
poly_fn := 53.68930043*t^8-.13e-6*t^7+1.000000000-102.8150104*t^6+.16e-8*t+.19e-6*t^5-13.20303455*t^2+61.36720611*t^4-.3e-7*t^3

>    poly_fn:=t->interp([-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1],[fRunge(-1),fRunge(-0.75),fRunge(-0.5),fRunge(-0.25),fRunge(0),fRunge(0.25),fRunge(0.5),fRunge(0.75),fRunge(1)],t):

>    plot([fRunge,poly_fn],-1..1,-1..1,thickness=4,color=[red,green],legend=["Runge's Function","8th order Polynomial"]);

[Maple Plot]

Section III: Cubic Spline Interpolation.

The function is now interpolated using cubic splines for the same 9 equidistant data points in the range of [-1,1] as used for polynomial interpolation.

>    fcubic:=spline([-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1],[fRunge(-1),fRunge(-0.75),fRunge(-0.5),fRunge(-0.25),fRunge(0),fRunge(0.25),fRunge(0.5),fRunge(0.75),fRunge(1)],t,cubic);

fcubic := PIECEWISE([.1009729044+.625113659000000044e-1*t+.787242339299999982*(t+1)^3, t < -.75],[.2239795199+.210119304600000001*t+.590431754647422702*(t+.75)^2-1.14501233899999999*(t+.75)^3, t < -.5]...

>    fcubic:=t->spline([-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1],[fRunge(-1),fRunge(-0.75),fRunge(-0.5),fRunge(-0.25),fRunge(0),fRunge(0.25),fRunge(0.5),fRunge(0.75),fRunge(1)],t,cubic):

>    plot([fRunge,fcubic],-1..1,-1..1,thickness=4,color=[red,blue],legend=["Runge's Function","Cubic Spline"]);

[Maple Plot]

Section IV: Comparison.

Below is a plot to compare the interpolated functions obtained using an 8th order polynomial and a cubic spline with the actual Runge's Function :

>    plot([fRunge,poly_fn,fcubic],-1..1,-1..1,thickness=4,color=[red,green,blue],title="Comparison of the 8th Order Polynomial and Cubic Spline with Runge's function",legend=["Runge's Function","8th order Polynomial","Cubic Spline"]);

[Maple Plot]

To better understand the difference between the 8th order polynomial and the cubic spline, let us compare the value of the functions with the original Runge's function at any point x which is not specified, say, x = 0.6.

Value from Runge's function at x=0.6

>    fRunge(0.6);

.1000000000

Value from polynomial interpolant at x=0.6

>    poly_fn(0.6);

.3049344276

Value from cubic spline interpolant at x=0.6

>    fcubic(0.6);

.1073282350

>   

Section V: Conclusion.

Maple helped us to apply our knowledge of numerical methods of interpolation to illustrate that for approximating Runge's function, cubic spline interpolation is better than polynomial interpolation.

Can you repeat the example by choosing 20 equidistant data points in [-1,1] and compare the results obtained from polynomial interpolation and spline interpolation for a value of x = -0.45?

References:

[1] Autar Kaw, Holistic Numerical Methods Institute, See http://numericalmethods.eng.usf.edu/mws/ind/05inp/mws_ind_inp_spe_needspline.pdf

Disclaimer:  While every effort has been made to validate the solutions in this worksheet, University of South Florida and the contributors are not responsible for any errors contained and are not liable for any damages resulting from the use of this material.