在 Julia 中动画化 ODE 的解决方案

Animating the solution to an ODE in Julia

我有一个 julia 代码:

using DifferentialEquations
using Plots
using ParameterizedFunctions
plotly()
lorenz = @ode_def Lorenz begin
  dx = σ*(y-x)
  dy = ρ*x-y-x*z
  dz = x*y-β*z
end σ = 10. β = 8./3. ρ => 28.
u0 = [1., 5., 10.]
tspan = (0., 2.)
prob = ODEProblem(lorenz, u0, tspan)
sol = solve(prob,save_timeseries=true)
plot(sol,vars=(:x,:y,:z))

结果是: next plot
我怎样才能为这个情节制作动画,使其可以在 REPL 和 jupyter 中运行?

对于DifferentialEquations.jl,有一个built-in动画函数可以处理这个问题。不幸的是,我意识到我忘了把它放在上一个版本中。当它发布时,语法将是(稍微简化你的代码):

using DifferentialEquations
using Plots
using ParameterizedFunctions
pyplot()
lorenz = @ode_def Lorenz begin
  dx = σ*(y-x)
  dy = ρ*x-y-x*z
  dz = x*y-β*z
end σ = 10. β = 8./3. ρ => 28.
u0 = [1., 5., 10.]
tspan = (0., 2.)
prob = ODEProblem(lorenz, u0, tspan)
sol = solve(prob)
animate(sol,vars=(:x,:y,:z),xlims=(-20,20),ylims=(-15,20),zlims=(10,40))

一些事情:animate 可以接受任何普通的绘图命令。但是,它在每一帧都从开始绘制到第 i 步,这意味着您可能需要手动设置轴以使其不左右移动。另一件需要注意的事情是我将后端切换到了 PyPlot。 Plotly 后端不能做动画。也许 PlotlyJS 可以? The animation function is documented here.

使用该命令将是迄今为止最简单的方法,但您可以使用 integrator interface. Essentially, you can just plot each step interval using this and get to the same place in the end. You'd have to use Plots.jl's animation interface.

“更手动地”执行此操作

编辑:如果您 Pkg.update() 现在应该可以使用了。