0% found this document useful (0 votes)
303 views8 pages

Numerical Methods With Matlab - ch10 - Solution

The following pages contain solutions to selected end-chapter Exercises. They are from the book Numerical Methods with Matlab: Implementations and Applications. The PDF version of the solutions may be downloaded or stored or printed only for noncommercial, educational use.

Uploaded by

Devesh Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
303 views8 pages

Numerical Methods With Matlab - ch10 - Solution

The following pages contain solutions to selected end-chapter Exercises. They are from the book Numerical Methods with Matlab: Implementations and Applications. The PDF version of the solutions may be downloaded or stored or printed only for noncommercial, educational use.

Uploaded by

Devesh Kumar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Selected Solutions for Exercises in

Numerical Methods with Matlab: Implementations and Applications Gerald W. Recktenwald Chapter 10 Interpolation

The following pages contain solutions to selected end-of-chapter Exercises from the book Numerical Methods with Matlab: Implementations and Applications, by Gerald W. Recktenwald, c 2001, Prentice-Hall, Upper Saddle River, NJ. The solutions are c 2001 Gerald W. Recktenwald. The PDF version of the solutions may be downloaded or stored or printed only for noncommercial, educational use. Repackaging and sale of these solutions in any form, without the written consent of the author, is prohibited. The latest version of this PDF le, along with other supplemental material for the book, can be found at www.prenhall.com/recktenwald.

Interpolation

10.3 What are the condition numbers for the two Vandermonde matrices in Examples 10.4 and 10.5? How does the change in (A) relate to the dierent results obtained in Example 10.5? Numerical Answer: For Example 10.4, (A) = 1.0 1031 . For Example 10.5, (A) = 3.8 103 .

10.3 (2+) In Example 10.4, the coecients of the interpolating polynomial are evaluated and printed to ve signicant digits. Evaluate the gasoline prices and the errors in the interpolant using the truncated coecients and the following denitions: Let (yi , pi ) be the year and price in the given tabulated data. Let pi be the price interpolated with the untruncated polynomial coecients at the yi , and pi be the price interpolated with the truncated coecients at the yi . In the absence of numerical errors we expect pi = pi = pi from the denition of interpolation. Compute and print pi , pi pi , p p 2 , pi , pi pi , and p p 2 . How many digits of the c coecients need to be retained in order to get p p comparable to p p ? Hint: The chop10 function in the utils directory of the NMM toolbox will be helpful. Partial Solution: The original and chopped coecients are:
chopped c(k) 3.03820000e-03 -3.02070000e+01 1.20140000e+05 -2.38900000e+08 2.37530000e+11 -9.44650000e+13

c(k) 3.03815522e-03 -3.02074596e+01 1.20137182e+05 -2.38896442e+08 2.37525874e+11 -9.44650495e+13

Evaluating the errors in the original polynomial (error p), and the polynomial obtained with chopped coecients (error pc) gives
year 1986 1988 1990 1992 1994 1996 price 133.50 132.20 138.70 141.50 137.60 144.20 error p 4.69e-02 -1.25e-02 -9.06e-02 -4.69e-02 -3.75e-02 9.69e-02 error pc 2.48e+10 2.49e+10 2.50e+10 2.51e+10 2.51e+10 2.52e+10 1.53e-01 6.13e+10

L2 norm of price error with original coefficients = L2 norm of price error with chopped coefficients =

Copyright c 2001, Gerald W. Recktenwald. Photocopying is permitted only for non-commercial educational purposes.

Chapter 10: Interpolation

10.12 Manually compute the quadratic interpolation in Example 10.7 using instead the following support points. (a) T1 = 20, T2 = 30, T3 = 40 (b) T1 = 0, T2 = 10, T3 = 20 Compare the results to the interpolant using T1 = 10, T2 = 20, and T3 = 30. Numerical Answer: Support Points T = 10, 20, 30 T = 20, 30, 40 T = 0, 10, 20 (22) 1.202 1.278 1.565 Dierence 0 6.3% 30%

10.14 The H2Osat.dat le in the data directory of the NMM toolbox contains saturation data for water. Use this data and quadratic polynomial interpolation to manually compute psat in the range 30 T 35 C. (a) Manually construct the divided-dierence table. Use the divDiffTable function to check your calculations. (b) Extract the coecients of the Newton interpolating polynomial from the divided dierence table. (c) Evaluate the interpolant at T = 32, 33, and 34 C. Verify you calculations with newtint. Partial Solution: Three support points are needed for quadratic interpolation. The data in H2Osat.dat is in increments of 5 C. Thus, reasonable support points for the interpolation are either T = 25, 30, and 35, or T = 30, 35, and 40. The table below summarizes the results of the interpolations. support points T = 25, 30, 35 T = 30, 35, 40 All pressures are in bar. psat (32) 0.04762 0.04754 psat (33) 0.05039 0.05030 psat (34) 0.05327 0.05322

Copyright c 2001, Gerald W. Recktenwald. Photocopying is permitted only for non-commercial educational purposes.

Interpolation

10.18 The degree of the polynomial interpolant used by lagrint and newtint is determined by the length of the input vectors to these functions. Suppose one wished to specify the order of the interpolant, regardless of the length of the input data. In other words, suppose the objective was to perform a local interpolation of degree n in a table of length m, where m > n. This requires selecting an appropriate subset of the input data table and passing this subset to lagrint or newtint. For example, a quadratic interpolation could be performed with
x = ... % define tabular data y = ... xhat = ... % interpolate at this value ibeg = ... % beginning index for support points of interpolant yhat = newtint(x(ibeg:ibeg+2),y(ibeg:ibeg+2),xhat)

Write an m-le function called quadinterp that automatically selects an appropriate subset of the vectors x and y, and returns the value of the quadratic interpolant using those support points. The function denition statement for quadinterp is
function yhat = quadinterp(x,y,xhat)

The quadinterp function calls newtint to perform the interpolation. The binSearch function in Listing 10.6 will be useful. Use your quadinterp function to generate data for smooth plot of the glycerin viscosity data in Example 10.2. Partial Solution: The selection of support points can be automated with the binSearch function from Listing 10.6. The binSearch function nds the index i such that x(i) xhat x(i + 1), when the x vector contains monotonically increasing values. The question remains: Which additional point is used to create the three support points needed to dene the quadratic interpolant? To avoid an index range error when i = 1, the three support pairs must be (x(1),y(1)), (x(2),y(2)), and (x(3),y(3)). Similarly, when i = n 1 the three support pairs must be (x(n-2),y(n-2)), (x(n-1),y(n-1)), and (x(n),y(n)). How should we choose the support points when 2 i n 2? Suppose we an attempt to optimize the choice of support points. (This turns out to be a bad idea, but well pursue it anyway.) Assume, for simplicity, that the x data are equally spaced, and consider the case, as depicted in the following sketch, where xhat is closer to x(i) than x(i+1)

xi1

xi

xi+1

xi+2

Exercise 1018. Attempt at optimal selection of support Points

1 (x +x ) 2 i i+1
Copyright c 2001, Gerald W. Recktenwald. Photocopying is permitted only for non-commercial educational purposes.

Chapter 10: Interpolation

For this xhat a good choice of support points would seem to be (x(i-1),y(i-1)), (x(i),y(i)), and (x(i+1),y(i+1)). On the other hand, if xhat > (x(i) + x(i + 1))/2 a good choice of support points would seem to be (x(i),y(i)), (x(i+1),y(i+1)), and (x(i+2),y(i+2)). The quadInterpBad function listed on the next page here uses this logic The demoQuadInterp function uses quadInterpBad and quadInterp functions to interpolate the glycerin viscosity data. Runninng demoQuadInterp gives the following plot.
12 Data Quadratic interpolant Discontinuous interpolant 10

(Pa s)

Exercise 1018. Two versions of piecewise quadratic interpolation


0 5 10 15 20 T ( C) 25 30 35 40 45 50

The problem with our attempt to optimize the choice of the support points is that the support points change in the middle of an interval, which causes the interpolant to be discontinuous. For this problem it is better to change the support points only at the ends of the intervals at the support points. The code to do this is actually simpler than the code in quadInterpBad. A better implementation of quadInterp involves choosing ibeg=i for all cases except when i=length(x)-1. Converting the code in quadInterpBad is left for the reader. On the next page are listings of the demoQuadInterp and quadInterpBad functions. An explicit loop is used to return a vector of interpolant values (yhat) if the input xhat is a vector. This is necessary since the binSearch function cannot be vectorized. The scalarQuadInterp subfunction performs the quadratic interpolation for a single value of xhat.

Copyright c 2001, Gerald W. Recktenwald. Photocopying is permitted only for non-commercial educational purposes.

6
function demoQuadInterp % demoQuadInterp Piecewise quadratic interpolation of glycerin data % Exercise 10-18 % --- Read data from glycerin.dat [t,D] = loadColData(glycerin.dat,5,7); mu = D(:,2); % viscosity is in second column of file % --- Evaluate interpolants ti = linspace(min(t),max(t)); mui = quadInterp(t,mu,ti); mu2 = quadInterpBad(t,mu,ti); plot(t,mu,ko,ti,mui,b-,ti,mu2,r--) legend(Data,Quadratic interpolant,Discontinuous interpolant); xlabel(T (^\circ C)); ylabel(\mu (Pa\cdot s));

Interpolation

function yhat = quadInterpBad(x,y,xhat) % quadInterpBad Discontinuous piecewise quadratic interpolation % in a table of (x,y) data. Exercise 10-18 % % Synposis: yhat = quadInterpBad(x,y,xhat) % % Input: x,y = vectors containing the tabulated data % xi = value of x at which function y = f(x) is desired % % Output: yi = value of y at xi obtained by quadratic interpolation % --- Allow vector of inputs: use explicit loop over all elements of xhat yhat = zeros(size(xhat)); for k=1:length(xhat) yhat(k) = scalarQuadInterp(x,y,xhat(k)); end % ============= function yhat = scalarQuadInterp(x,y,xhat) % scalarQuadInterp Piecewise quadratic interpolation at a single point % % Use hueristic choice of three support points. Let i be the index % returned from binSearch, i.e. x(i) <= xhat <= x(i+1). If i is in % the range 2 <= i <= n-1, and if xhat is less than the midpoint of % [x(i), x(i+1)], then choose support points [i-1, i, i+1]. Otherwise % choose support points [i, i+1, i+2]. i = binSearch(x,xhat); % Find appropriate data pair if i==1 ibeg=1; elseif i==length(x)-1 ibeg=length(x)-2; % Avoid index range error elseif xhat < (x(i)+0.5*(x(i+1)-x(i))) % hueristic choice of support points ibeg = i-1; else ibeg = i; end yhat = newtint(x(ibeg:ibeg+2),y(ibeg:ibeg+2),xhat); % Evaluate interpolant

Copyright c 2001, Gerald W. Recktenwald. Photocopying is permitted only for non-commercial educational purposes.

Chapter 10: Interpolation 10.21 The classic example of polynomial wiggle is the so-called Runge function r(x) = 1 , 1 + 25x2

named after German mathematician Carl Runge (18561927), who used this function to study the behavior of high-degree polynomial interpolation. Write a function m-le called runge to perform the following tasks. (a) Compute n equally spaced xk values (k = 1, . . . , n) on the interval 1 x 1. Let n be an input parameter to runge. (b) Evaluate r(xk ) from Equation (10.55) for k = 1, . . . , n. (c) Use the n pairs of (xk , r(xk )) values to dene a degree n 1 polynomial interpolant, Pn1 . Evaluate the interpolant at xj , j = 1, . . . , 100 points in the interval 1 x 1. x x (d) Plot a comparison of the original data, (xk , r(xk )), the interpolant, (j , Pn1 (j ), and the true x value of the function at the interpolating points (j , r(j ). Use open circles for r(xk ), a dashed x x x line for Pn1 (j ), and a solid line for r(j ). x (e) Print the value of r() Pn1 () 2 . x x Run your runge function for n=5:2:15, and discuss the behavior of r() Pn1 () x increased.
2

as n is

Partial Solution: The runge function is listed below. Running the function and interpretting the results are left to the reader.
function e = runge(n) % runge Uniformly spaced interpolation of the Runge function. Exercise 10-21 % % Synopsis: runge(n) % % Input: n = number of points to define the polynomial interpolant. % The polynomial is of degree n-1 % % Output: e = L2 norm of error for the interpolant evaluated at % 100 points in the interval -1 <= x <= 1 x = linspace(-1,1,n); r = 1./(1+25*x.^2); xhat = linspace(-1,1); yhat = newtint(x,r,xhat); rhat = 1./(1+25*xhat.^2); % % % % % Define n support points (x,r(x)) Exact value of Runge function 100 equally-spaced points Polynomial interpolant of degree n-1 Exact r(x) at xhat

plot(x,r,o,xhat,yhat,--,xhat,rhat,-); e = norm(yhat-rhat);

Copyright c 2001, Gerald W. Recktenwald. Photocopying is permitted only for non-commercial educational purposes.

Interpolation

10.24 The stdatm.dat in the data directory of the NMM toolbox gives the properties of the socalled standard atmosphere as a function of elevation above sea level. Write a routine that uses piecewise linear interpolation to return the values of T , p, and at any elevation in the range of the table. Write your m-le function so that it does not read the data from this le when it is executed; that is, store the data in matrix variables in your m-le. What are the values of T , p, and at z = 5500 m and z = 9023 m? Plot the variation of T and in the range 0 z 15 km. Numerical Answer: z (m) 5500 9023 T ( C) 17.47 43.56 p (Pa) 54050 30701 10.32 Plot the spline interpolant of the following data: xi yi 0.2 0.5535 0.6 1.0173 1.0 1.0389 1.4 0.8911 1.8 0.7020 2.2 0.5257 (kg/m3 ) 0.7364 0.4659

On the same plot, compare the spline interpolant to y = create the data in the table.

12.5 x exp( 1.5 x), which was used to

Partial Solution: Below is a plot of the sample data, the spline interpolant, and the function used to create the sample data. Lacking any information about the slope at the endpoints, the not-a-knot end conditions were used to dene the spline.

1.2 Data notaknot spline Original function

1.1

0.9 y 0.8 0.7

0.6

Plot of solution to Exercise 1032.


0.5 1 x 1.5 2 2.5

0.5 0

Copyright c 2001, Gerald W. Recktenwald. Photocopying is permitted only for non-commercial educational purposes.

You might also like