# Polyfit and polyval
import numpy as np
import matplotlib.pyplot as plt
x = range(5)
y = [1,2,1,3,5]
p2 = np.polyfit(x,y,2)
p4 = np.polyfit(x,y,4)
xx = np.linspace(-1,5,400)
plt.plot(xx,np.polyval(p2,xx),label='fitting polynomial of degree 2',c="r")
plt.plot(xx,np.polyval(p4,xx),label='interpolating polynomial of degree 4',c="g")
plt.plot(x,y,'o',c="b")
plt.axis([-1,5,0,6])
plt.legend(loc='upper left',fontsize='small')
plt.grid()
plt.xlabel("x")
plt.ylabel("y")
plt.savefig('ex201.png', dpi=72)
plt.show()

Discover more from Tips and Hints for Aerospace Engineers

Subscribe now to keep reading and get access to the full archive.

Continue reading