【问题标题】:Julia Plots; How can I increase number of samples/data points?朱莉娅情节;如何增加样本/数据点的数量?
【发布时间】:2018-10-27 23:44:59
【问题描述】:

求解微分方程并绘制结果时,如何增加绘制的数据点数量?我有

using DifferentialEquations
using Plots

function lorenz(du,u,p,t)
    du[1] = 10.0*(u[2]-u[1])
    du[2] = u[1]*(28.0-u[3]) - u[2]
    du[3] = u[1]*u[2] - (8/3)*u[3]
end

u0 = [5.0;0.0;0.0]
tspan = (0.0,100000.0)
prob = ODEProblem(lorenz,u0,tspan)

sol = solve(prob)

plot(sol, vars = 1, xlims = (10,100000),xscale =:log)

具体来说,当使用 PyPlots 之类的东西时,我可以使用:

x = linspace(0,100000,num=10000)

我将 num = 10000 设置为增加了样本数量并允许更高的数据点分辨率以实现更长的积分时间跨度。

显而易见的答案是使用 PyPlots,但我不确定是否可以将 PyPlots 与 DifferentialEquations 包一起使用。很高兴知道这是如何为 Plots 完成的。 (根据函数的不同,有些图非常参差不齐)。

【问题讨论】:

  • 我在@ChrisRackauckas 对采样部分发表评论后编辑了答案。请查看最新版本。

标签: julia differential-equations plots.jl differentialequations.jl


【解决方案1】:

Plots.jl 实际上是围绕绘图后端的包装器,例如 PyPlot.jlGR.jl(它们也是包装器)。

如果您安装了PyPlot.jl,您可以通过调用pyplot() 使Plots.jl 使用PyPlot。

using Plots
pyplot() # switch to PyPlot
plot(sol.t[2:end], sol[1,2:end], xlim =(10,100000), xscale=:log10)

请注意,我从第二个索引开始,因为在第一个索引处 t 为 0,这会产生对数刻度的问题(即使设置了 xlim)。这将使用解决方案中的每个数据点。如果您在终端上,这将打开 PyPlot GUI,因此您可以随意缩放。如果您使用的是 Juno,您可以使用 gui(plot(...)) 打开窗口。

您可以在任意时间点对系统进行采样。 (使用DifferentialEquations' interpolant sol(t)

t = range(10, stop = 100000, num=10000) # to reach t=100000 you need to adjust `maxiters` accordingly in your call to `solve`
samples = sol(t) # sample using interpolator
plot(samples.t, samples.u[1,:], xscale=:log10)

您也可以使用不带对数刻度和 plotdensity 选项的配方。

plot(sol, vars=1, plotdensity=1000)

更多示例请参见此处:http://diffeq.sciml.ai/stable/basics/plot.html

【讨论】:

  • 对于 PyPlot 部分,我建议使用插值 sol(t),其中 t = range(0,stop=100000,length=10000) 作为问题中所要求的真正对应物。但是,是的,使用带有plotdensity 的绘图配方是首选选项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-23
  • 1970-01-01
  • 2018-11-27
  • 2015-02-19
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
相关资源
最近更新 更多