Docsity

Interpolation Polynomials: Procedures and Examples using Maple, Study notes of Mathematical Methods for Numerical Analysis and Optimization

Toggle sidebar
bg1
Interpolation Polynomials
Mth 351 Spring 2001
Bent E. Petersen
Filename: 351s2001_interp_polys.mws
In this worksheet I present a few procedures that are useful in experimenting with interpolation
polynomials.
> restart;
finterp() interpolates a function, given the (distinct) nodes, by computing the function values at the
nodes and submitting the resulting lists of abscissas and ordinates to Maple interp() function. Note that
Maple allows sending any number of unspecified arguments to a procedure, so it is a simple matter to
pass lists od data of arbitrary length to a procedure. All the variables passed to the procedure may be
accessed through the list args - the nth entry in the list is args[n] - the sublist consisting of the kth to the
nth entry is args[k..n]. Note args is not an array, in spite of appearances. The number of entries in the
list of parameters is given by nargs.
> finterp:=proc(f,x)
> if nargs < 3 then ERROR(FAIL); fi;
> interp([args[3..nargs]],map(f,[args[3..nargs]]),x);
> end:
Here's a quick test of finterp().
> ps:=finterp(sin,x,0,Pi/4,Pi/3,Pi/2,2*Pi/3,3*Pi/4,Pi):
> plot(sin(x)-ps,x=0..Pi, axes=NORMAL,thickness=3,title="Error in
ps",numpoints=100,resolution=300);
Page 1
pf3
pf4
pf5

Partial preview of the text

Download Interpolation Polynomials: Procedures and Examples using Maple and more Study notes Mathematical Methods for Numerical Analysis and Optimization in PDF only on Docsity!

Interpolation Polynomials

Mth 351 Spring 2001

Bent E. Petersen Filename: 351s2001_interp_polys.mws

In this worksheet I present a few procedures that are useful in experimenting with interpolation polynomials.

> restart;

finterp() interpolates a function, given the (distinct) nodes, by computing the function values at the nodes and submitting the resulting lists of abscissas and ordinates to Maple interp() function. Note that Maple allows sending any number of unspecified arguments to a procedure, so it is a simple matter to pass lists od data of arbitrary length to a procedure. All the variables passed to the procedure may be accessed through the list args - the nth entry in the list is args[n] - the sublist consisting of the kth to the nth entry is args[k..n]. Note args is not an array, in spite of appearances. The number of entries in the list of parameters is given by nargs.

**> finterp:=proc(f,x)

if nargs < 3 then ERROR(FAIL); fi; interp([args[3..nargs]],map(f,[args[3..nargs]]),x); end:**

Here's a quick test of finterp().

> ps:=finterp(sin,x,0,Pi/4,Pi/3,Pi/2,2Pi/3,3Pi/4,Pi):

plot(sin(x)-ps,x=0..Pi, axes=NORMAL,thickness=3,title="Error in ps",numpoints=100,resolution=300);**

We see here the typical error distribution for uniformly distributed nodes, so we have confidence in finterp().

If the coefficients in ps are evaluated in floating point above, the accuracy will be reduced quite a bit because the values of ps are quite sensitive to roundoff (and other) errors in the coefficients. Thus I have written finterp(), and the other routines below to use symbolic calculations (unless floating point data is received). This is inefficient, of course, but allows us to maintain accuracy.

Here's a simple routine, Unodes(), which returns a list (not a formal list in the sense of data types) of n+1 uniformly spaced nodes, in order, for an interval [a,b]. Thus the endpoints determine n equal subintervals.

**> Unodes:=proc(n,a,b)

local L, k; if n < 1 then ERROR(FAIL); fi; L:=[]; for k from 0 to n do L:=[op(L),a+k(b-a)/n]; od; op(L); end:*

Note Unodes() returns the nodes symbolically if possible. We return op(L) in Unodes() so we get the actual nodes. Returning L would return a single data item, a list of the nodes. This is not just a matter

It's rather precise! Let's convert it to floating point so we can see what it looks like.

> evalf(pu,16);

โˆ’ .001296680939813 x^6 + .01222092994370 x^5 โˆ’ .00641254180168 x^4 โˆ’.16073505149962 x^3

โˆ’ .00284509716638 x^2 +1.00053876843603 x > plot(sin(x)-pu,x=0..Pi,axes=BOXED,thickness=3,title="Error in pu",numpoints=100,resolution=300);

The symbolic Chebyshev nodes are fairly complicated here. If we use them to compute the exact interpolation polynomial we obtain an expression that covers many pages and consumes a great deal of RAM. Maple is likely to choke on this expression, or at least be very slow. Thus we convert the Chebyshev nodes to floating point with 16 decimal digits before computing the interpolation polynomial.

> pc:=finterp(sin,x,evalf(Cnodes(6,0,Pi),16));

pc := โˆ’.001285392572 x^6 + .01211453953 x^5 โˆ’ .006033382849 x^4 โˆ’.1613673246 x^3

โˆ’ .002364997394 x^2 + 1.000423400 x โˆ’.

Generally there is no telling in what order the terms in a polynomial will returned. We can put them in standard order by using the function sort().

> sort(pc,x);

โˆ’.001285392572 x^6 + .01211453953 x^5 โˆ’ .006033382849 x^4 โˆ’.1613673246 x^3

โˆ’ .002364997394 x^2 + 1.000423400 x โˆ’. > plot(sin(x)-pc,x=0..Pi,axes=BOXED,thickness=3,title="Error in pc",numpoints=100,resolution=300);

As we expected the Chebyshev nodes give a better distributed error. Moreover the maximum errors are smaller than in the uniform nodes case.

>