x=np.arange(1,13,1)
y=np.array([17, 19, 21, 28, 33, 38, 37, 37, 31, 23, 19, 18 ])
plt.plot(x,y)
plt.show()
可以看出溫度是以周期為12的正弦函數
#構建函數y=a*sin(x*pi/6+b)+c
#使用optimize.curve_fit函數求出a、b、c的值
x=np.arange(1,13,1)
x1=np.arange(1,13,0.1)
ymax=np.array([17, 19, 21, 28, 33, 38, 37, 37, 31, 23, 19, 18 ])
def fmax(x,a,b,c):
return a*np.sin(x*np.pi/6+b)+c
fita,fitb=optimize.curve_fit(fmax,x,ymax,[1,1,1]) #[1,1,1]是初始化的參數
print(fita) #參數
print(fitb) #參數的協方差矩陣
plt.plot(x,ymax)
plt.plot(x1,fmax(x1,fita[0],fita[1],fita[2]))
plt.show()