""" Exercise 5.18 from "A primer on..." fit a polynomial to the data read from file in Ex. 5.16, using the NumPy functions polyfit and poly1d. """ import matplotlib.pyplot as plt import numpy as np #import the function we wrote in exercise 5.16: from read_density_data import read_density_file #a) def fit(x, y, deg): for d in deg: coeff = np.polyfit(x, y, d) p = np.poly1d(coeff) y_fitted = p(x) plt.plot(x,y_fitted,label=f'deg={d}') plt.plot(x, y,'ro', label='data') plt.legend() plt.show() #b) if __name__ == '__main__': temp, dens = read_density_file('density_water.txt') fit(temp, dens, [1,2]) """ Terminal> python fit_density_data.py (output is a plot) """