ODE solution with scipy.integrate.ode and scipy.integrate.odeint gives different results

I am trying to solve a simple ODE using the odeint and ode SciPy methods. An example that I use is the spread of the disease (later I will try to solve my more complex ODEs). The function should look like this:

enter image description here

My odeint code gives acceptable results.

from scipy import *
import scipy.integrate as integ
from pylab import * 

def dydt(y,t):
    K=0.00003
    L=250000
    return K*y*(L-y)

t=linspace(0,12,61)
y=integ.odeint(dydt, 250, t)
plot(t,y)
show()

But when I try to do the same with the ode method, I get a completely different one (and the wrong results and plot seem to show this). What am I doing wrong?

from scipy.integrate import ode
from pylab import * 
from numpy import *

y0, t0 = 250, 0

def dydt(y,t):
    K=0.00003
    L=250000
    return K*y*(L-y)

r = ode(dydt).set_integrator('vode', method='bdf')
r.set_initial_value(y0, t0)

t1 = 10.0
dt = 0.1

sol = []

while r.successful() and r.t < t1:
    r.integrate(r.t + dt)
    sol.append([r.t, r.y])

sol = array(sol)
plot(sol[:,0],sol[:,1])
show()

Note that ry contains real values ​​and therefore I could only draw the real part.

+4
source share

Source: https://habr.com/ru/post/1622575/


All Articles