Docsity

Comparing Lagrange and Chebyshev Interpolation Methods, Exercises of Mathematics

Toggle sidebar
bg1
9Polynomial
Interpolation
Lab Objective: Learn and compare three methods of polynomial interpolation: standard Lagrange
interpolation, Barycentric Lagrange interpolation and Chebyshev interpolation. Explore Runge’s phe-
nomenon and how the choice of interpolating points affect the results. Use polynomial interpolation
to study air polution by approximating graphs of particulates in air.
Polynomial Interpolation
Polynomial interpolation is the method of finding a polynomial that matches a function at specific
points in its range. More precisely, if f(x)is a function on the interval [a, b]and p(x)is a poly-
nomial then p(x)interpolates the function f(x)at the points x0, x1, . . . , xnif p(xj) = f(xj)for all
j= 0,1, . . . , n. In this lab most of the discussion is focused on using interpolation as a means of
approximating functions or data, however, polynomial interpolation is useful in a much wider array
of applications.
Given a function f(x)and a set of unique points {x}n
i=0, it can be shown that there exists a
unique interpolating polynomial p(x). That is, there is one and only one polynomial of degree n
that interpolates f(x)through those points. This uniqueness property is why, for the remainder of
this lab, an interpolating polynomial is referred to as the interpolating polynomial. One approach to
finding the unique interpolating polynomial of degree nis Lagrange interpolation.
Lagrange interpolation
Given a set {xi}n
i=1 of npoints to interpolate, a family of nbasis functions with the following
property, is constructed:
Lj(xi) = (0if i6=j
1if i=j.
Once each of the npolynomials in this family of basis functions is known, they can be combined
with the y-values yi=f(xi)of the function to be interpolated in the following manner:
p(x) =
n
X
j=1
yjLj(x)(9.1)
This will create the unique interpolating polynomial.
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Comparing Lagrange and Chebyshev Interpolation Methods and more Exercises Mathematics in PDF only on Docsity!

Polynomial

Interpolation

Lab Objective: Learn and compare three methods of polynomial interpolation: standard Lagrange interpolation, Barycentric Lagrange interpolation and Chebyshev interpolation. Explore Runge’s phe- nomenon and how the choice of interpolating points affect the results. Use polynomial interpolation to study air polution by approximating graphs of particulates in air.

Polynomial Interpolation

Polynomial interpolation is the method of finding a polynomial that matches a function at specific points in its range. More precisely, if f (x) is a function on the interval [a, b] and p(x) is a poly- nomial then p(x) interpolates the function f (x) at the points x 0 , x 1 ,... , xn if p(xj ) = f (xj ) for all j = 0, 1 ,... , n. In this lab most of the discussion is focused on using interpolation as a means of approximating functions or data, however, polynomial interpolation is useful in a much wider array of applications. Given a function f (x) and a set of unique points {x}ni=0, it can be shown that there exists a unique interpolating polynomial p(x). That is, there is one and only one polynomial of degree n that interpolates f (x) through those points. This uniqueness property is why, for the remainder of this lab, an interpolating polynomial is referred to as the interpolating polynomial. One approach to finding the unique interpolating polynomial of degree n is Lagrange interpolation.

Lagrange interpolation

Given a set {xi}ni=1 of n points to interpolate, a family of n basis functions with the following property, is constructed:

Lj (xi) =

0 if i 6 = j 1 if i = j

Once each of the n polynomials in this family of basis functions is known, they can be combined with the y-values yi = f (xi) of the function to be interpolated in the following manner:

p(x) =

∑^ n

j=

yj Lj (x) (9.1)

This will create the unique interpolating polynomial.

1

2 Lab 9. Polynomial Interpolation

1.00 0.75 0.50 0.25 0.00 0.25 0.50 0.75 1.

(a) Interpolation using 5 equally spaced points.

1.00 0.75 0.50 0.25 0.00 0.25 0.50 0.75 1.

(b) Interpolation using 11 equally spaced points.

Figure 9.1: Interpolations of Runge’s function f (x) = (^) 1+25^1 x 2 with equally spaced interpolating points.

The Lagrange form of this family of basis functions is

Lj (x) =

∏^ n

k=1,k 6 =j

(x − xk)

∏^ n

k=1,k 6 =j

(xj − xk)

Each of these Lagrange basis functions is of degree n − 1 and has the necessary properties as given above. Lagrange interpolation consists of computing the Lagrange basis functions then combining them with the y-values. Since polynomials are typically represented in their expanded form with coefficients on each of the terms, it may seem like the best option when working with polynomials would be to use Sympy, or NumPy’s poly1d class to compute the coefficients of the interpolating polynomial individually. This is rarely the best approach, however, since expanding out the large polynomials that are required can quickly lead to instability (especially when using large numbers of interpolating points). Instead, it is usually best just to leave the polynomials in unexpanded form (which is still a polynomial, just not a pretty-looking one), and compute values of the polynomial directly from this unexpanded form.

Evaluate the polynomial (x-2)(x+1) at 10 points without expanding the ←↩

expression. >>> pts = np.arange(10) >>> (pts - 2) * (pts + 1) array([ 2, 0, 0, 2, 6, 12, 20, 30, 42, 56])

In the given example, there would have been no instability if the expression had actually been expanded but in the case of a large polynomial, stability issues can dominate the computation. Although the coefficients of the interpolating polynomials will not be explicitly computed in this lab, polynomials are still being used, albeit in a different form.

Problem 1. Write a function that uses the Lagrange method to find an interpolating polyno- mial for a set of data points and evaluates the calculated polynomial at specified values. This

4 Lab 9. Polynomial Interpolation

which is the first barycentric form. The computation of w(x) can be avoided by first noting that

1 = w(x)

∑^ n

j=

wj x − xj

which allows the interpolating polynomial to be rewriten as

p(x) =

∑^ n

j=

wj yj x − xj ∑n

j=

wj x − xj

This form of the Lagrange interpolant is known as the second barycentric form which is the form used in Barycentric Lagrange interpolation. So far, the changes made to Lagrange interpolation have resulted in an algorithm that is O(n) once the barycentric weights (wj ) are known. The following adjustments will improve the algorithm so that it is numerically stable and later discussions will allow for the quick addition of new interpolating points after startup.

The second barycentric form makes it clear that any factors that are common to the wk can be ignored (since they will show up in both the numerator and denominator). This allows for an important improvement to the formula that will prevent overflow error in the arithmetic. When com- puting the barycentric weights, each element of the product

∏n k=1,k 6 =j (xj^ −^ xk)^ should be multiplied by C−^1 , where 4 C is the width of the interval being interpolated (C is known as the capacity of the interval). In effect, this scales each barycentric weight by C^1 −n^ which helps to prevent overflow during computation. Thus, the new barycentric weights are given by

wj =

∏n k=1,k 6 =j [(xj^ −^ xk)C]^

Once again, this change is possible since the extra factor C^1 −n^ is cancelled out in the final product. This process is summed up in the following code:

Given a NumPy array xint of interpolating x-values, calculate the weights.

>>> n = len(xint) # Number of interpolating points. >>> w = np.ones(n) # Array for storing barycentric weights.

Calculate the capacity of the interval.

>>> C = (np.max(xint) - np.min(xint)) / 4

>>> shuffle = np.random.permutation(n-1) >>> for j in range(n): >>> temp = (xint[j] - np.delete(xint, j)) / C >>> temp = temp[shuffle] # Randomize order of product. >>> w[j] /= np.product(temp)

The order of temp was randomized so that the arithmetic does not overflow due to poor ordering (if standard ordering is used, overflow errors can be encountered since all of the points of similar magnitude are multiplied together at once). When these two fixes are combined, the Barycentric Algorithm becomes numerically stable.

Problem 2. Create a class that performs Barycentric Lagrange interpolation. The constructor of your class should accept two NumPy arrays which contain the x and y values of the interpo- lation points. Store these arrays as attributes. In the constructor, compute the corresponding barycentric weights and store the resulting array as a class attribute. Be sure that the relative ordering of the arrays remains unchanged. Implement the call() method so that it accepts a NumPy array of values at which to evaluate the interpolating polynomial and returns an array of the evaluated points. Your class can be tested in the same way as the Lagrange function written in Problem 1

Achtung!

As currently explained and implemented, the Barycentric class from Problem 2 will fail when a point to be evaluated exactly matches one of the x-values of the interpolating points. This happens because a divide by zero error is encountered in the final step of the algorithm. The fix for this, although not required here, is quite easy: keep track of any problem points and replace the final computed value with the corresponding y-value (since this is a point that is exactly interpolated). If you do not implement this fix, just be sure not to pass in any points that exactly match your interpolating values.

Another advantage of the barycentric method is that it allows for the addition of new interpolat- ing points in O(n) time. Given a set of existing barycentric weights {wj }nj=1 and a new interpolating point xi, the new barycentric weight is given by

wi = ∏n^1 k=1(xi^ −^ xk)^

In addition to calculating the new barycentric weight, all existing weights should be updated as follows wj = (^) xjw −jxi.

Problem 3. Include a method in the class written in Problem 2 that allows for the addition of new interpolating points by updating the barycentric weights. Your function should accept two NumPy arrays which contain the x and y values of the new interpolation points. Update and store the old weights then extend the class attribute arrays that store the weights, and the x and y values of the interpolation points with the new data. When updating all class attributes, make sure to maintain the same relative order.

The implementation outlined here calls for the y-values of the interpolating points to be known during startup, however, these values are not needed until run-time This allows the y-values to be changed without having to recompute the barycentric weights. This is an additional advantage of Barycentric Lagrange interpolation.

Scipy’s Barycentric Lagrange class

Scipy includes a Barycentric interpolator class. This class includes the same functionality as the class described in Problems 2 and 3 in addition to the ability to update the y-values of the interpolation points. The following code will produce a figure similar to Figure 9.1b.

is increased. One such set of points is the Chebyshev extremal points which are related to the Chebyshev polynomials (to be discussed shortly). The n + 1 Chebyshev extremal points on the interval [a, b] are given by the formula yj = 12 (a + b + (b − a) cos( jπn )) for j = 0, 1 ,... , n. These points are shown in Figure 9.3. One important feature of these points is that they are clustered near the endpoints of the interval, this is key to preventing Runge’s phenomenon.

Problem 4. Write a function that defines a domain x of 400 equally spaced points on the interval [− 1 , 1]. For n = 2^2 , 23 ,... , 28 , repeat the following experiment.

  1. Interpolate Runge’s function f (x) = 1/(1+25x^2 ) with n equally spaced points over [− 1 , 1] using SciPy’s BarycentricInterpolator class, resulting in an approximating function f˜. Compute the absolute error ‖f (x) − f˜ (x)‖∞ of the approximation using la.norm() with ord=np.inf.
  2. Interpolate Runge’s function with n + 1 Chebyshev extremal points, also via SciPy, and compute the absolute error.

Plot the errors of each method against the number of interpolating points n in a log-log plot. To verify that your figure make sense, try plotting the interpolating polynomials with the original function for a few of the larger values of n.

Figure 9.3: The Chebyshev extremal points. The n points where the Chebyshev polynomial of degree n reaches its local extrema. These points are also the projection onto the x-axis of n equally spaced points around the unit circle.

Chebyshev Polynomials

The Chebyshev roots and Chebyshev extremal points are closely related to a set of polynomials known as the Chebyshev polynomials. The first two Chebyshev polynomials are defined as T 0 (x) = 1 and T 1 (x) = x. The remaining polynomials are defined by the recursive algorithm Tn+1(x) = 2 xTn(x) − Tn− 1 (x). The Chebyshev polynomials form a complete basis for the polynomials in R

8 Lab 9. Polynomial Interpolation

which means that for any polynomial p(x), there exists a set of unique coefficients {ak}nk=0 such that

p(x) =

∑^ n

k=

akTk.

Finding the Chebyshev representation of an interpolating polynomial is a slow process (domi- nated by matrix multiplication or solving a linear system), but when the interpolating values are the Chebyshev extrema, there exists a fast algorithm for computing the Chebyshev coefficients of the interpolating polynomial. This algorithm is based on the Fast Fourier transform which has temporal complexity O(n log n). Given the n + 1 Chebyshev extremal points yj = cos( jπn ) for j = 0, 1 ,... , n and a function f , the unique n-degree interpolating polynomial p(x) is given by

p(x) =

∑^ n

k=

akTk

where ak = γk< [DF T (f (y 0 ), f (y 1 ),... , f (y 2 n− 1 ))]k.

Note that although this formulation includes yj for j > n, there are really only n + 1 distinct values being used since yn−k = yn+k. Also, < denotes the real part of the Fourier transform and γk is defined as

γk =

1 k ∈ { 0 , n} 2 otherwise.

Problem 5. Write a function that accepts a function f and an integer n. Compute the n + 1 Chebyshev coefficients for the degree n interpolating polynomial of f using the Fourier transform (np.real() and np.fft.fft() will be helpful). When using NumPy’s fft() function, multiply every entry of the resulting array by the scaling factor (^21) n to match the derivation given above. Validate your function with np.polynomial.chebyshev.poly2cheb(). The results should be exact for polynomials.

Define f(x) = -3 + 2x^2 - x^3 + x^4 by its (ascending) coefficients.

>>> f = lambda x: -3 + 2x2 - x3 + x* >>> pcoeffs = [-3, 0, 2, -1, 1] >>> ccoeffs = np.polynomial.chebyshev.poly2cheb(pcoeffs)

The following callable objects are equivalent to f().

>>> fpoly = np.polynomial.Polynomial(pcoeffs) >>> fcheb = np.polynomial.Chebyshev(ccoeffs)

Lagrange vs. Chebyshev

As was previously stated, Barycentric Lagrange interpolation is O(n^2 ) at startup and O(n) at runtime while Chebyshev interpolation is O(n log n). This improved speed is one of the greatest advantages of Chebyshev interpolation. Chebyshev interpolation is also more accurate than Barycentric inter- polation, even when using the same points. Despite these significant advantages in accuracy and

10 Lab 9. Polynomial Interpolation

Additional Material

The Clenshaw Algorithm is a fast algorithm commonly used to evaluate a polynomial given its representation in Chebyshev coefficients. This algorithm is based on the recursive relation between Chebyshev polynomials and is the algorithm used by NumPy’s polynomial.chebyshev module.

Algorithm 9.1 Accepts an array x of points at which to evaluate the polynomial and an array a = [a 0 , a 1 ,... , an− 1 ] of Chebyshev coefficients.

1: procedure ClenshawRecursion(x, a) 2: un+1 ← 0 3: un ← 0 4: k ← n − 1 5: while k ≥ 1 do 6: uk ← 2 xuk+1 − uk+2 + ak 7: k ← k − 1 8: return a 0 + xu 1 − u 2