使用DifferentialEquations.jl Julia包时出现方法错误

时间:2017-09-13 20:10:23

标签: error-handling type-conversion julia differential-equations differentialequations.jl

我正在尝试使用DifferentialEquation.jl包来解决ode45微分方程,但是我遇到了一个方法错误。

using DifferentialEquations

M = 400; m = 35;
C = 3e3; c = 300;
K = 50e3; k = 200e3;
A = 0.05; L = 0.5; vh = 13.9

MM = [M 0;
      0 m] # mass matrix

CC = [C -C;
     -C C+c] # damping matrix

KK = [K -K;
     -K K+k] # stiffness matrix

w(t) = A*sin(2*pi*vh*t/L)
wd(t) = A*2*pi*vh*t/L*cos(2*pi*vh*t/L) # dw/dt

n = 2 # number of (original) equations
Mi = MM^(-1)
AA = [zeros(n,n) eye(n); -Mi*KK -Mi*CC]

f(t) = [0; c*wd(t)+k*w(t)] # force vector
g(t,y) = AA*y+[zeros(n,1); Mi*f(t)]

y0 = zeros(4) # initial conditions
tspan = (0.0, 0.5) # time span

prob = ODEProblem(g, y0, tspan) # defining the problem
solve(prob) 

代码会出现错误:

  

MethodError:不能convert类型为Array {Float64,2}的对象到类型为Array {Float64,1}的对象   这可能是由对构造函数Array {Float64,1}(...)的调用引起的,   因为类型构造函数会回退转换方法。

我不知道我可能做错了什么,虽然我认为错误可能与y0有关(因为typeof(y0)= Array {Float64,1})并且错误发生在解决的行中()函数是。

事先感谢你的帮助!

1 个答案:

答案 0 :(得分:2)

未经测试的预感:改变:

g(t,y) = AA*y+[zeros(n,1); Mi*f(t)]

为:

g(t,y) = AA*y+[zeros(n); Mi*f(t)]

前者将创建一个具有一列的二维矩阵。后者将创建一维向量。您看到的错误信息是它在一个需要一维的地方获得二维数组。