HW9 Interpolation
Find the degree-5 polynomial p(x) = co + c₁x + c₂x² + 3x³ + ₁x² + c5³ that exactly passes the six given points with coordinates (-9.,9), (-8., 6), (-5., -2), (-2., 1), (1.,-3), (7.,5). You need to form the interpolation problem as solving a square linear equation Ax = b, using the standard polynomial basis 1, æ, x², ..., x5. The matrix A is a Vandermonde matrix formed from the x-coordinates of the data points, the b vector comes from the y-coordinates of the data points. (In the lecture slides interpolate.pdf the matrix is denoted as V and the solution vector denoted as C, but there is no essentially difference besides notation. The Ax = bis actually the more commonly used notation. The solution vector X contains the coefficients of the interpolation polynomial: x = [co, C1, C2, C3, C4, C5]. Screenshot(Alt + A) Note the x has nothing to do with the x in the polynomial p(x). If we denote c as the array containing the coefficients, c = [co, C1, C2, C3, C4, C5], then c is exactly the x from the linear equation Ax = b. For ease of comprehension, in the input we use c to denote the coefficient vector, that is c[0] =co, c[1] =C1, …, c[5] = C5. Download and complete this file interpolate.py. When the coordinates of the data points are given as above, find the condition number of the Vandermonde matrix A and the computed coefficients of the interpolation polynomial. cond(A)= type your answer... c[0]= type your answer... c[1] = type your answer... c[2]= type your answer... c[3]= type your answer... c[4]= type your answer... c[5] = type your answer... (Note: round all numbers to exactly 3 decimal digits, do not omit ending zero, and do not omit starting zero as well. I.e., 0.001 should be input as 0.001, not as .001)
Slightly modify the data points by changing only the x-coordinate of the first point, that is, (-9., 9) is changed into (-8.2, 9), with other data points unchanged. Do exactly the same as for the previous problem: Find the condition number of the Vandermonde matrix A and the computed coefficients of the interpolation polynomial. cond(A)= type your answer... c[0]= type your answer... c[1]= type your answer... c[2]= type your answer... c[3]= type your answer... c[4]= type your answer... c[5]= type your answer... (Note: round all numbers to exactly 3 decimal digits, do not omit ending zero, and do not omit starting zero as well. I.e., 0.001 should be input as 0.001, not as .001)
Your completed file interpolate.py should also make three plots, similar to 50 40 30 20 10 0 -10 -20 * data points interpolation -10.0 -7.5 -5.0 -2.5 0.0 2.5 5.0 7.5
Y 70 60 50 40 30 20 10 0 -10 -10.0 -7.5 -5.0 -2.5 X 0.0 2.5 5.0 data points 2 interpolation 2 7.5
60 40 20- 0 -20- -10.0 -7.5 -5.0 -2.5 0.0 2.5 5.0 ★ data points data points 2 interpolation - interpolation 2 7.5 Observe that the seemingly small change in just one data point completely changes the shape of the interpolation polynomial. This shows that interpolation, which passes all data points, may easily become unstable. In contrast, the least-squares approach does not require exact match of each data point, but strives for overall minimum approximation error in 2-norm, which makes it generally more stable. In machine learning, the interpolation approach is associated with the term "over fitting".
interpolate.py 11 12 13 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 14 15 16 import numpy as np 17 import scipy.linalg as linalg 18 import matplotlib.pyplot as plt 19 20 21 22 23 24 25 26 27 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 This problem asks you to plot data points, then apply linear regression and quadratic regression to predict the engin efficiency in the requested years 62 63 64 65 www #. # note: interpolation is essentially solving Ax=b. the difference with least-squares is # that the A matrix here is square, while in least-squares A is not squre. # # for this problem the solution x will be the coefficients of the interpolation polynomial. # note: a polynomial is uniquely determined by its coefficients. # def poly_interpolate (xData, yData): # # Goal: find the coefficients of the polynomial that passes the points with coordinates # specified by pairs in (xData, yData), return the coefficients in a numpy array # # Approach: genetate A and b for the interpolation problem, then solve Ax=b by calling # linalg. solve() to get the solution x, which contains the coefficients of the # interpolation polynomial. (with the corfficients, you should be able to construct # this polynomial for plotting etc.) # # before return, print out the condition number of the vandermonde matrix A, # print out also the values of the polynomial coefficients, # round all numbers to exactly 3 decimal digits using the .3f format # def plot_interpolate(): xData = np.array([-9., -8., -5., -2., 1., 7. ]) yData = np.array([9, 6, -2, 1, -3, 5 ]) xvect = np.linspace (min (xData)-1, max(xData)+1, 200) # # add code to plot the raw data points whose coordinates are contained in (xData, yData) # # add code to plot your interpolation polynomial evaluated on the vector xvect: # 1st you need to call the above function to get the coefficients, then you evaluate #the polynomial on xvect and plot the polynomial. # note: on your plot, your polynomial should exactly pass all data points, similar to the 1st sample plot # #
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 #====== 105 # if your code above is correct, you do not need any modification below 106 107 if _name__== '__main__': 108 109 119 # # add code to do the same things above for a slightly modified dataset (xData2, yData) # the modification is by changing only the xData [0] element slightly # note1: you also need the cond (A) and the coefficients for this dataset for canvas input # note2: your plot should be similar to the 2nd sample plot # xData2 = xData.copy(); xData2 [0]=-8.2 ##note yData is not changed ## observe that this seemingly small change in xData [0] completely changes the shape ## of the interpolation polynomial. ## this shows that interpolation, which passes all data points, may easily become unstable, ## in contrast the least-squares approach is generally more stable # # add code to plot the two datasets and their interpolation polynomials in the same figure. # note1: you do not need extra computation if you save the two interpolations in different arrays. # note2: your plot should be similar to the 3rd sample plot # #in your combined plot, you can set a larger figsize than the default, say figsize=(15,12), # so that close-by data points are easier to see plot_interpolate () ==========
HW9 Interpolation
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am