gpt4 book ai didi

error-handling - 使用DifferentialEquations.jl Julia程序包时的方法错误

转载 作者:行者123 更新时间:2023-12-03 07:49:27 24 4
gpt4 key购买 nike

我正在尝试使用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: Cannot convert an object of type Array{Float64,2} to an object of type Array{Float64,1} This may have arisen from a call to the constructor Array{Float64,1}(...), since type constructors fall back to convert methods.



尽管我相信错误可能与y0有关(因为typeof(y0)= Array {Float64,1}),并且错误发生在resolve()函数所在的行中,但我没有得到可能做错的事情是。

预先感谢您的帮助!

最佳答案

未经测试的预感:更改:

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

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

前者将创建一列的二维矩阵。后者将创建一维 vector 。您看到的错误消息是它在期望一维数组的地方得到了二维数组。

关于error-handling - 使用DifferentialEquations.jl Julia程序包时的方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46206060/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com