我试图用scipy.integrate.RK45()
来解一个一阶微分方程组。我已经编写了我希望绘制的模型函数(位移与时间),但是RK45()
要求这个函数需要两个参数,即't‘和'y',其中t是标量,'y’是数组。下文对此作了更好的说明:
https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.RK45.html
我的脚本如下:
import numpy as np
from scipy.integrate import RK45, RK23
def model(t, y):
m = 2
c = 10
k = 1500
if (t >= 0 and t < 0.1):
F = 200 * t
if (t >= 0.1 and t < 0.25):
F = 20
else:
F = 0
E_matrix = [[0, 1], [(-k / m), (-c / m)]]
Q_matrix = [0, F / m]
return E_matrix * X + Q_matrix
time_step = 0.01
t_upper = 0.5
t_lower = 0
initial_conditions = [0, 0]
points_to_plot = RK45(fun=model(t, y), t0=t_lower, y0=initial_conditions, t_bound=t_upper, vectorized=True)
下面是我想要解决的系统的图片:
由于大多数解决方案都使用odeint()
,所以我发现很少有这样的例子。
这两个参数(t,y)是什么,我如何有效地将它们合并到我的函数中?
发布于 2020-09-18 11:18:27
您已经使用了t
。现在,将def model(t, y):
更改为def model(t, X):
,您也将使用X
。注意,t和y是位置参数,您可以在函数中任意调用它们。您还有另一个问题,就是乘法Python列表!在Python中,与Matlab不同,您需要指定创建一个数组:
变化
E_matrix = [[0, 1], [(-k / m), (-c / m)]]
Q_matrix = [0, F / m]
至
E_matrix = np.array([[0, 1], [(-k / m), (-c / m)]])
Q_matrix = np.array([0, F / m])
和
return E_matrix*X + Q_matrix
至
return E_matrix @ X + Q_matrix
因为@
是NumPy中的矩阵积。*
执行按元素划分的产品.
编辑:我没有发现RK45(fun=model(t, y),
的电话。通过这样做,您将在t,y
传递函数模型的值。您需要给出函数本身:RK45(fun=model, ...
https://stackoverflow.com/questions/63953924
复制相似问题