简体   繁体   中英

Differential Equations - ODEINT

I have to solve two differential equations by ODEINT in Python, the equations:

y''(t) = (l*q)/a * (1/y(p) * [1 - z'(p)*u]
z''(t) = a * (1/y(p) * y'(p)*u 

So I was told to make:

y1=y
y2=y'
z1=z
z2=z'

and

y1' = y2
y2' = y'' = (l*q)/a * (1/y(p) * [1 - z'(p)*u]
z1' = z2
z2' = z''(t) = a * (1/y(p) * y'(p)*u

and now I have to solve these 4 equations. l , q , a , u are known.

I tried something like this:

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

def rownanie(y, t, l, q, a, u):
    y1, y2, z1, z2 = y
    dydt = [y2, ((l*q)/a)*(1/y1)*(1-z2*u), z2, (a*y2*u)/y1]
    return dydt

l = 1
q = 1
a = 10
u = 0.25

y0 = 0
z0 = 0
t = np.linspace(0, 10, 101)
sol = odeint(rownanie, y0, z0, t, args=(l,q,a,u))
print(sol)

Need help with this

If you read the docs , you'll see odeint

Solves the initial value problem for stiff or non-stiff systems of first order ode-s:

dy/dt = func(y, t, ...) [or func(t, y, ...)]

where y can be a vector

This conversion is a standard mathematical way of transforming a second order ODE into a first order vector ODE.

You therefore create a new vector variable (I'll call it Y to avoid confusion), consisting of the vector Y = [y, y_prime, z, z_prime] : Your implementation of the function is correct.

Also note that in order to be solved numerically you need to specify the initial conditions of all the vector, in this case y0, z0, y'0 and z'0. As Thomas pointed out, you need to specify these values as the initial value of the vector when you call odeint .

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

def rownanie(Y, t, l, q, a, u):
    y1, y2, z1, z2 = Y
    dydt = [y2, ((l*q)/a)*(1/y1)*(1-z2*u), z2, (a*y2*u)/y1]
    return dydt

l = 1
q = 1
a = 10
u = 0.25

y0 = 0
z0 = 0
y0_prime, z0_prime = 0, 0  # you need to specify a value for these too
t = np.linspace(0, 10, 101)
sol = odeint(rownanie, [y0, y0_prime, z0, z0_prime], t, args=(l,q,a,u))
print(sol)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM