Search code examples
pythonnumpymatplotlibscipydifferential-equations

the size of array does not match


i have this problem when i try to run my code "The size of the array returned by func (1) does not match the size of 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()

what can i do to make it better and thanks in advance


Solution

  • You mostly forgot to reteurn from your function. For me now it works.

    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()