### Exercise 5.18 ### Fit a polynomial to data points ### OCL import matplotlib.pyplot as plt import numpy as np # Below is a function for showing a scatter plot of y versus x # together with the best fit to the data (in the least squares sense) # using a polynomial of degree deg. If deg is a list with more # than one element, then the best fit to the data using a # polynomial of degree deg[0], the best fit using a polynomial # of degree deg[1], etc, will all be plotted. def fit(x, y, deg): # We first make a scatter plot of y versus x plt.plot(x, y, 'bo') # To plot a polynomial p(x), we first define some x-values # (say a= 2 and words[0] != '#': temp.append(float(words[0])) dens.append(float(words[1])) infile.close() fit(temp, dens, deg=[1,2]) plt.title(fnames[i]) plt.xlabel('Temperature') plt.ylabel('Density') plt.show()