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()
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]
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] ])
, ie y[1]
is put as y
in dy/dt
which gives dy[1]/dt = y[2]
. The second element, ie a*y[0]+b*y[1]
serves as func(y,t0,...)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.