from scipy.integrate import odeint
import numpy as np
from scipy.optimize import leastsq
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif'] = ['SimHei'] #这两句用来正常显示中文标签
plt.rcParams['axes.unicode_minus'] = False
t = np.arange(2001,2021,1)
def deriv(w,t,a,b,c,d,e,f):
x,y = w
return np.array((a-b*x-c*y)*x, (d-e*y-f*x)*y)
p=[1.8772, 0.0103, 1.4380, 1.0334, 0.0096, 0.8475, 42.2, 0.8463]
a,b,c,d,e,f,x0,y0=p
yinit = np.array([x0,y0]) # 初值
yyy = odeint(deriv,yinit,t,args=(a,b,c,d,e,f))
plt.figure(figsize=(7,5))
plt.plot(t,yyy[:,0],"b-",label="$x_1$变化曲线")
plt.plot(t,ec2['tfe'],"black",label="$x_1$实际曲线")
plt.plot(t,yyy[:,1],"r-",label="$x_2$变化曲线")
plt.plot(t,ec2['re'],"b",label="$x_2$实际曲线")
#plt.plot([1978,2050],[17,250],"g--")
#plt.plot([0,100],[375,375],"g--")
plt.xlabel(u'时间t')
plt.ylabel(u'能源消费量')
plt.title(u'传统化石能源和可再生能源的演化曲线')
>
plt.legend(loc=4)
plt.show()
RuntimeError Traceback (most recent call last)
<ipython-input-67-8df6742403e2> in <module>
22 a,b,c,d,e,f,x0,y0=p
23 yinit = np.array([x0,y0]) # 初值
---> 24 yyy = odeint(deriv,yinit,t,args=(a,b,c,d,e,f))
25 plt.figure(figsize=(7,5))
26 plt.plot(t,yyy[:,0],"b-",label="$x_1$变化曲线")
~\anaconda\lib\site-packages\scipy\integrate\odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg, tfirst)
239 t = copy(t)
240 y0 = copy(y0)
--> 241 output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
242 full_output, rtol, atol, tcrit, h0, hmax, hmin,
243 ixpr, mxstep, mxhnil, mxordn, mxords,
RuntimeError: The size of the array returned by func (1) does not match the size of y0 (2).
我觉得是不是这个微分方程需要将二阶的转为一阶的,但是我转不过来这个弯,不知道怎么写了。
# 逻辑关系没有看
# 12行 是不是少了个括号,加个括号可以运行过去
return np.array(((a-b*x-c*y)*x, (d-e*y-f*x)*y))
...
# NameError: name 'ec2' is not defined
我没看逻辑哈 不过这个报错说的明显是矩阵size不匹配 你可以逐一check一下你写的每个数据矩阵的size是不是满足你的函数调用需求