import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
plt.grid(linestyle='--') #设置网格线---这里是虚线
X = np.array([0,2,4,5,6,7,8,9,10.5,11.5,12.5,14,16,17,18,19,20,21,22,23,24])
Y = np.array([2,2,0,2,5,8,25,12,5,10,12,7,9,28,22,10,9,11,8,9,3])
xnew= np.linspace(0,24,24*60)
# 引入插值函数,这里的点较散乱,所以我选取高阶的样条插值
f=interp1d(X,Y,'quadratic'); ynew=f(xnew)
plt.plot(xnew,ynew,c='b')
plt.show()
# 计算一天的车辆总数
c=sum(ynew)
print('一天总共:',c)
import matplotlib.pyplot as plt
import numpy as np
from numpy import polyfit,polyval
x=np.linspace(0,10,11)[1:11]
y=np.array([2615,1943,1494,1087,765,538,484,290,226,204])
p=polyfit(x,y,2)
print('p:',p)
ynew=polyval(p,x)
plt.plot(x,ynew)
plt.show()
ypred=polyval(p,4.5)
print('使用4.5年后轿车的平均价格大致为:',ypred)
import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint
def fT(y,x):
f,df,ddf,T,dT=y; #拆分、降阶
return np.array([df,ddf,2*(df*df)-T-3*f*ddf,dT,-2.1*f*dT])
x=np.arange(0,10,0.1)
ynew=odeint(fT,[0.0,0.0,0.68,1.0,-0.5],x)
#plt.rc('font',size=16);plt.rc('font',family='Times New Roman')
plt.plot(x,ynew[:,0],label='f')
plt.plot(x,ynew[:,3],label='T')
plt.legend()
plt.show()
import matplotlib.pyplot as plt
import numpy as np
x=np.linspace(0,8,9)
y=np.array([5,5.9945,7.0932,8.2744,9.5073,10.7555,11.9804,13.1465,14.2247])
plt.scatter(x,y,c='r',label='Data point')
p=np.polyfit(x,y,1)
print('p为:',p)
ynew1=np.polyval(p,x)
plt.plot(x,ynew1)
plt.legend()
plt.show()
#从图中可以看出一次多项式的拟合效果就很好,所以接下来预测时采用一次多项式即可
y9=np.polyval(p,9)
y10=np.polyval(p,10)
print('t=9野兔的数量:',y9)
print('t=10野兔的数量:',y10)
抢讲座
链接:https://pan.baidu.com/s/1kcr1LHINF87ni91bJfpW1g
取码:o9j2