"""Ex. 5.19 from 'A primer on...' Use numpy tools to fit polynomials to the data read from file in 5.17. For convenience we import the function from 5.17 and use it for reading the data. """ import numpy as np import matplotlib.pyplot as plt import sys from read_density_data import read_density_data def fit(x,y,deg): for d in deg: coeff = np.polyfit(x,y,d) poly = np.poly1d(coeff) y_fitted = poly(x) plt.plot(x,y_fitted,label=f'deg={d}') plt.plot(x,y,'o',label='data') plt.legend() plt.show() if __name__ == "__main__": temp, dens = read_density_data(sys.argv[1]) fit(temp,dens,[1,2]) """ Terminal> python fit_density_data.py density_water.txt (output is a plot) """