在Julia中使用DifferentialEquations包来解决矩阵ODE

时间:2017-07-19 14:22:31

标签: matrix julia ode differentialequations.jl

我想解决:

[\ mathbf {M} \ ddot {\ mathbf {U}} + \ mathbf {C} \ dot {\ mathbf {U}} + \ mathbf {K} \ mathbf {U} = \ mathbf {P} (T)]

或者,以状态空间形式:

[\点{\ mathbf {Y}} = F(\ mathbf {Y},T)]

其中:

[\ mathbf {Y} = \ left [\ begin {array} {c} \ mathbf {U} \ \ dot {\ mathbf {U} \ end {array} \ right]]

[f(\ mathbf {Y},t)= \ left [\ begin {array} {c} \ dot {\ mathbf {U}} \ \ mathbf {M} ^ { - 1} \ mathbf {P }(t) - \ mathbf {M} ^ { - 1} \ mathbf {C} \ dot {\ mathbf {U}} - \ mathbf {M} ^ { - 1} \ mathbf {K} \ mathbf {U} \ end {array} \ right]]

我在Julia中使用

尝试了以下代码

\ mathbf {M} = \ left [\ begin {array} {cc} 2& 0 \ 0& 1 \ end {array} \ right];

\ mathbf {C} = \ left [\ begin {array} {cc} 0& 0 \ 0& 0 \ end {array} \ right];

\ mathbf {K} = \ left [\ begin {array} {cc} 96& -32 \ -32& 32 \ end {array} \ right];

\ mathbf {P}(t)= \ left [\ begin {array} {c} 10 \ 10 \ end {array} \ right]

using DifferentialEquations
function eq(t,u,du)
    v=reshape([u...],Int(length(u)/2),2)
    du=reshape([v[:,2];-[10;10]-M\C*v[:,2]-M\K*v[:,1]],length(u))
end
u0=[0;0;0;0];
tspan=(0.0,10.0);
prob=ODEProblem(eq,u0,tspan)
sol=solve(prob)

但运行这些代码行会导致此错误:

ERROR: InexactError()

我正在使用Julia ver。 0.5.2。

请帮帮我。谢谢。

1 个答案:

答案 0 :(得分:3)

您的问题是DifferentialEquations.jl尊重您的输入类型。

u0=[0;0;0;0];

这是一个整数数组,所以这意味着你的问题将演变成一个整数数组。在第一步中,它发现计算返回浮点数,因此它不知道要做什么u,因为你说它必须是一个整数数组。解决这个问题的方法是说你的问题出在浮点数上:

u0=[0.0;0.0;0.0;0.0];

现在进展正确。

但是,让我们迈出新的一步。 DifferentialEquations.jl尊重您的输入类型,因此,只需将初始条件设为矩阵,DifferentialEquations.jl就会使其成为矩阵的问题。如果您将u0设为矩阵,则无需重新整形:

u0=[0.0 0.0
    0.0 0.0]

一旦你这样做,你只需编写一个直接适用于矩阵的方程式。例如:

using DifferentialEquations
M = 1
K = 1
C = 1
function eq(t,u,du)
  du .= u[:,2] .- 10 - M./C.*u[:,2] - M.\K.*u[:,1]
end
u0=[0.0 0.0
    0.0 0.0]
tspan=(0.0,10.0);
prob=ODEProblem(eq,u0,tspan)
sol=solve(prob)

我不确定我是否得到了你想要解决的等式,因为它很难读,但这应该让你非常接近你想要的。