【问题标题】:the size of array does not match数组大小不匹配
【发布时间】:2021-01-31 22:17:43
【问题描述】:

当我尝试运行我的代码时遇到了这个问题 "func(1)返回的数组大小与y0(2)的大小不匹配"

import matplotlib.pyplot as plt
import numpy as np
from scipy.integrate import odeint

def F(X,t):
    dX_1 = X[0]*(1-X[1]) #proie
    dX_2 = 1.1*X[1]*(X[0]-1) #prédateur

t0 = 0                            # temps initial
tfinal = 20                       # temps final
t = np.linspace(t0, tfinal, 1000) # n=1000
H = [[1.01,1.01], [1,2], [1,3], [1,4], [1,5]] # conditions initiales 

for i in range (len(H)):
    solution=odeint(F, H[i], t)
    x = solution[:,0]
    y = solution[:,1]
    plt.figure(1)
    plt.grid(True)
    plt.suptitle("modèle de Lotka-volterras")
    plt.plot(x,y)
    plt.xlabel('proie')
    plt.ylabel('prédateur')
plt.show()

plt.figure(2)
sol = odeint(F,[1,2],t)
plt.grid(True)
plt.suptitle("modèle de Lotka-volterras")
plt.plot(t,sol[:,0])
plt.plot(t,sol[:,1])
plt.legend(["proie","prédateur"])
plt.xlabel('temps')
plt.ylabel('x(t),y(t)')
plt.show()

我该怎么做才能让它变得更好,并在此先感谢

【问题讨论】:

  • 根据 Python 风格指南 PEP-8,您可以做的第一件事是使用 4 个空格进行缩进。您现在的代码几乎无法阅读。

标签: python numpy matplotlib scipy differential-equations


【解决方案1】:

您大多忘记了从您的职能部门返回。对我来说,它现在有效。

from scipy.integrate import odeint
def F(X,t):
    dX_1=X[0]*(1-X[1]) #proie
    dX_2=1.1*X[1]*(X[0]-1) #prédateur
    return dX_1,dX_2
t0=0 #temps initial
tfinal=20 #temps final
t=np.linspace(t0,tfinal,1000) #n=1000
H=[[1.01,1.01],[1,2],[1,3],[1,4],[1,5]]#conditions initiales 
for i in range (len(H)):
    solution=odeint(F,H[i],t)
    x=solution[:,0]
    y=solution[:,1]
    plt.figure(1)
    plt.grid(True)
    plt.suptitle("modèle de Lotka-volterras")
    plt.plot(x,y)
    plt.xlabel('proie')
    plt.ylabel('prédateur')
plt.show()
plt.figure(2)
sol=odeint(F,[1,2],t)
plt.grid(True)
plt.suptitle("modèle de Lotka-volterras")
plt.plot(t,sol[:,0])
plt.plot(t,sol[:,1])
plt.legend(["proie","prédateur"])
plt.xlabel('temps')
plt.ylabel('x(t),y(t)')
plt.show()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-11
    • 1970-01-01
    • 2015-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-21
    • 2018-07-28
    相关资源
    最近更新 更多