Julia 中使用微分方程的二阶 ODE

2nd Order ODEs in Julia using DifferentialEquations

我正在尝试使用 Julia 中的微分方程求解谐波振荡器。即:

using DifferentialEquations
using Plots

m = 1.0                          
ω = 1.0                     

function mass_system!(ddu,du,u,p,t)
    # a(t) = (1/m) w^2 x 
    ddu[1] = (1/m)*(ω^2)*u[1]
end

v0 = 0.0                     
u0 = 1.0                  
tspan = (0.0,10.0)               

prob = SecondOrderODEProblem{isinplace}(mass_system!,v0,u0,tspan,callback=CallbackSet())
sol = solve(prob)

但是好像没看懂ODE的构造函数。在 运行 时,我得到:

ERROR: LoadError: TypeError: non-boolean (typeof(isinplace)) used in boolean context
Stacktrace:
 [1] #_#219(::Base.Iterators.Pairs{Symbol,CallbackSet{Tuple{},Tuple{}},Tuple{Symbol},NamedTuple{(:callback,),Tuple{CallbackSet{Tuple{},Tuple{}}}}}, ::Type{SecondOrderODEProblem{DiffEqBas
e.isinplace}}, ::Function, ::Float64, ::Float64, ::Tuple{Float64,Float64}, ::DiffEqBase.NullParameters) at /Users/brandonmanley/.julia/packages/DiffEqBase/avuk1/src/problems/ode_problems
.jl:144
 [2] Type at ./none:0 [inlined] (repeats 2 times)
 [3] top-level scope at /Users/brandonmanley/Desktop/nBody/nBodyNN/test.jl:25
 [4] include at ./boot.jl:328 [inlined]
 [5] include_relative(::Module, ::String) at ./loading.jl:1105
 [6] include(::Module, ::String) at ./Base.jl:31
 [7] exec_options(::Base.JLOptions) at ./client.jl:287
 [8] _start() at ./client.jl:460

有什么想法吗?

我强烈建议您查看 some of the tutorials. You have a few mistakes here which are addressed in this tutorial on classical physics models。具体来说,如果您选择无法更改的状态变量(即标量),则不应使用就地修改函数。如果是这种情况,只需在生成输出的地方使用不合适的形式。看起来像:

using DifferentialEquations
using Plots

m = 1.0                          
ω = 1.0                     

function mass_system!(du,u,p,t)
    # a(t) = (1/m) w^2 x 
    (1/m)*(ω^2)*u[1]
end

v0 = 0.0                     
u0 = 1.0                  
tspan = (0.0,10.0)               

prob = SecondOrderODEProblem(mass_system!,v0,u0,tspan)
sol = solve(prob)