You are on page 1of 2

Lab 5, Interpolation and Approximation

Topics
Mathematics: polynomial interpolation; divided dierence tables. MATLAB: more practice with input and output arguments for functions; MATLABs interpolation tools; nested for loops.

Preparation
Read your lecture notes. See also Chapra and Canales account of divided dierences and polynomial interpolation (Section 8.1, pages 474486). In this lab you will work through two of their exercises about engineering applications of interpolation (Exercises 44 and 24 from Chapter 20). You will need an extra function M-le for this lab session: mypolyint.m uses divided dierences to construct an interpolation polynomial of specied degree. This M-le is available from the Sample M-les on the course webpage. Read the help for this M-le to see how to use it.

Linear interpolation
Polynomial interpolation is used to estimate function values between precise data points (Chapra and Canale, page 474). Dynamic viscosity of water () is related to temperature (T ) in the following manner. T ( C ) (103 N.s/m2 ) 0 1.787 5 1.519 10 1.307 20 1.002 30 0.7975 40 0.6529

1. How many data points are needed to construct a linear interpolation polynomial? 2. Choose the appropriate points from the above table and use linear interpolation and mypolyint.m to nd the approximate value of at T = 7.5 C . 3. Using the same points as in Question 2, plot the linear approximation you have just used, along with all the data points. (Use mypolyint.m again, but this time let xi be a vector spanning the desired plotting range.) Make sure that your plot agrees with your answer to Question 2.

Higher degree interpolation


4. How many data points are needed to construct a quadratic interpolation polynomial? Choose the appropriate points from the above table, and use quadratic interpolation to nd the approximate value of at T = 7.5 C . 5. Plot the quadratic approximation found in Question 4, along with all the data points. 6. Repeat Question 4 using interpolation polynomials of degree 3, 4 and 5. Checkpoint: What is your value for at T = 7.5 C ? For what range of T values would you expect the quadratic interpolation polynomial to give a good estimate for ? 3

Interpolation and approximation for noisy data


As Chapra and Canale say (page 440), if there are signicant errors in your data, its not usually a good idea to use interpolation. In such cases we use least squares regression: instead of looking for a straight line (say) that passes through just two points, we look for a line that passes as close as possible to all the data points at the same time. The following table shows shows rainfall data for the catchment of a river, and ow data for the river itself. We would like to predict the river ow if the rainfall was 120 cm. Rainfall (cm) Flow (m3 /s) 88.9 14.6 94 16.1 99.1 16.6 104.1 15.3 108.5 16.7 116.8 18.1 127 19.5 139.7 23.2

MATLAB has several built-in interpolation and approximation tools. Read the help for the commands polyfit and polyval. Checkpoint: Use these commands to nd the straight line which gives the best t to the data points. Use this line to calculate an estimate for the river ow if the rainfall was 120 cm. Use the same commands to plot the straight line and the data points together.

Practice test question


In this nal task you are to write a function M-le mydivdiff.m which will construct the table of divided dierences for any set of N data points. To help you do this, here is a template for the start of the function M-le, and a diagram and mathematical formulae for calculating Ai,j , the (i, j ) entry in the table of divided dierences. Note that, although all matrices in MATLAB must be rectangular, only the upper-left half of A is actually used. Think carefully about the order in which you will calculate the entries in A, and the starting and nishing values for i and j in your for loops (the nishing value for j is not always the same). Remember that your function doesnt know in advance how many data points there are, so you will need to use the length command to nd out the number of entries in x. function A = mydivdiff(x, f) % INPUTS: x - Nx1 vector of x values at which data for the function is specified % f - Nx1 vector of corresponding function values % % OUTPUTS: A - (N-1)x(N-1) table of divided differences j A * * * * * * * * * * * * * * *

x * * * * * *

f * * * * * *

Ai,1 =

fi+1 fi , xi+1 xi

Ai,j =

Ai+1,j 1 Ai,j 1 xi+j xi

Checkpoint: Use your program to calculate the table of divided dierences for the viscosity temperature data given above.

You might also like