简体   繁体   English

微分方程-ODEINT

[英]Differential Equations - ODEINT

I have to solve two differential equations by ODEINT in Python, the equations: 我必须使用Python的ODEINT解决两个微分方程,这些方程是:

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. lqau是已知的。

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 如果您阅读文档 ,将会看到odeint

Solves the initial value problem for stiff or non-stiff systems of first order ode-s: 解决一阶ode-s的刚性或非刚性系统的初值问题:

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

where y can be a vector y可以是向量

This conversion is a standard mathematical way of transforming a second order ODE into a first order vector ODE. 这种转换是将二阶ODE转换为一阶向量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. 因此,您将创建一个新的向量变量(为避免混淆,我将其称为Y),由向量Y = [y, y_prime, z, z_prime] :函数的实现是正确的。

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. 还要注意,为了进行数值求解,您需要指定所有向量的初始条件,在这种情况下为y0,z0,y'0和z'0。 As Thomas pointed out, you need to specify these values as the initial value of the vector when you call odeint . 正如Thomas指出的那样,在调用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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