问题描述
我正在尝试使用Julia中的DifferentialEquations解决谐波振荡器.即:
I'm trying to solve the harmonic oscillator using DifferentialEquations in Julia. i.e.:
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构造函数.跑步后,我得到:
But it doesn't seem to understand the ODE constructor. Upon running, I get:
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
有什么想法吗?
推荐答案
我强烈建议您查看一些教程.您在这里遇到了一些错误,这些错误在本经典物理学模型教程中的 .具体来说,如果您选择了一个无法突变的状态变量(即标量),则不应使用就地修改功能.如果是这种情况,只需使用异位表单即可生成输出.看起来像:
I would highly recommend looking at some of the tutorials. You have a few mistakes here which are addressed in this tutorial on classical physics models. Specifically, you shouldn't use an in-place modifying function if you choose a state variable that cannot be mutated, i.e. a scalar. If that's the case, just use the out-of-place form where you generate the output. That looks like:
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)
这篇关于朱莉娅使用微分方程的二阶ODE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!