import numpy as np
import matplotlib.pyplot as plt
import math
import pandas as pd
from scipy.optimize import curve_fit
from scipy import log
#表示函数表达式
def logistic_increase_function(t,K,P0,r):
P0 = 35
K = 1722
t0 = 0
exp_value=np.exp(r*(t-t0))
return (K*exp_value*P0)/(K+(exp_value-1)*P0)
#键入日期以及草履虫数
t=[0, 1, 2, 3, 4, 5, 6, 7, 8]
P=[35, 50, 120, 210, 650, 1230, 1620, 1710, 1760]
#创建向量类型
t=np.array(t)
P=np.array(P)
# 用最小二乘法估计拟合
popt, pcov = curve_fit(logistic_increase_function, t, P)
#获取popt里面是拟合系数
# print("K:环境容纳量 P0:初始草履虫数量 r:瞬时增长速率 t:时间")
'''
生物知识补充:
K为环境容量,即增长到最后,P(t)能达到的极限。
P0为初始容量,就是t=0时刻的数量。
r为增长速率,r越大则增长越快,越快逼近K值,r越小增长越慢,越慢逼近K值。
'''
print(popt)
#拟合后预测的P值
P_predict = logistic_increase_function(t,popt[0],popt[1],popt[2])
#未来预测
# future=[11,18,19,20 ,21, 22, 23, 24, 25, 26, 27,28,29,30,31,41,51,61,71,81,91,101]
# future=np.array(future)
# future_predict=logistic_increase_function(future,popt[0],popt[1],popt[2])
#近期情况预测
# tomorrow=[28,29,30,32,33,35,37,40]
# tomorrow=np.array(tomorrow)
# tomorrow_predict = logistic_increase_function(tomorrow,popt[0],popt[1],popt[2])
#绘图
plot1 = plt.plot(t, P, 's',label="confimed infected people number")
plot2 = plt.plot(t, P_predict, 'r',label='predict infected people number')
# plot3 = plt.plot(tomorrow, tomorrow_predict, 's',label='predict infected people number')
plt.xlabel('time')
plt.ylabel('confimed infected people number')
plt.legend(loc=0) #指定legend的位置右下角
# print(logistic_increase_function(np.array(28),popt[0],popt[1],popt[2]))
# print(logistic_increase_function(np.array(29),popt[0],popt[1],popt[2]))
plt.show()
#未来预测绘图
#plot2 = plt.plot(t, P_predict, 'r',label='polyfit values')
#plot3 = plt.plot(future, future_predict, 'r',label='polyfit values')
#plt.show()
print("Program done!")
正在思考中,,,,,,,,,
最新推荐文章于 2022-03-06 20:22:00 发布