4

I am a bit confused with odeint.

I found one example below to solve y"=ay + by'. So it seems that y[0] is the function, y[1] is the first derivative.

So does the following expression mean y[1] =y' and y'[1]= a*y[0]+b*y[1] ?

If it were y[2], a*y[0]+b*y[1], what would it mean?

I am a bit confused since the expression does not say the left hand side of the equation.

I also encountered expressions like [a(y[0], y[1]), b(y[0], y[1])] but have no clue of the differential equation.

Here is one example:

from scipy.integrate import odeint
from pylab import * # for plotting commands

def deriv(y,t): # return derivatives of the array y
    a = -2.0
    b = -0.1
    return array([ y[1], a*y[0]+b*y[1] ])

time = linspace(0.0,10.0,1000)
yinit = array([0.0005,0.2]) # initial values
y = odeint(deriv,yinit,time)
figure()
plot(time,y[:,0])
xlabel('t')
ylabel('y')
show()
ovgolovin
  • 13,063
  • 6
  • 47
  • 78
pappu
  • 129
  • 2
  • 3
  • 8
  • I found the answer, the equations should be represented in the following way: y1'= y2 , y2'=y3, .., yn'=F(x,..) and only right hand sides of the equations have to be given for solving the differential equation. – pappu Feb 06 '12 at 21:30

2 Answers2

2

Let's use Y in deriv instead of y for the rest of answer to be clear:

def deriv(Y,t): # return derivatives of the array Y
    a = -2.0
    b = -0.1
    return array([ Y[1], a*Y[0]+b*Y[1] ])

Function deriv takes Y = [y, y'] as the input.

And it should output their derivatives ([y', y'']).

y' = Y[1]

y'' = a*Y[0]+b*Y[1]

ovgolovin
  • 13,063
  • 6
  • 47
  • 78
1

Read the documentation on odeint. It requires as an input equation of the following sort:

dy/dt = func(y,t0,...)

As far as I understand it, first element of array([ y[1], a*y[0]+b*y[1] ]), i.e. y[1] is put as y in dy/dt which gives dy[1]/dt = y[2]. The second element, i.e. a*y[0]+b*y[1] serves as func(y,t0,...)

Max Li
  • 5,069
  • 3
  • 23
  • 35