【发布时间】:2021-05-10 00:17:25
【问题描述】:
我很难和 Julia 一起做动画。
我有一个函数 sf(t)=t^(1/2) 从 tmin=0 到 tmax=40,我不明白如何从 tmin 到 tmax 逐步显示解决方案。我使用网上找到的 Julia 文档和教程尝试了多种方法,但它们几乎都使用预定义的函数(主要是 sin 和 cos)。 Julia 使用以下代码返回的错误是 MethodError: no method matching getindex(::typeof(sf), ::Int64)。我有两个问题:为什么会出现这个错误?和有没有比循环访问索引更简单的方法来创建动画?
谢谢
using Plots
tmin=0
tmax=40
tvec= range(tmin,tmax,length=100)
# I define the function here, but I'm not sure this is really needed
function sf(t)
t^(1/2)
end
# I create an "empty" plot to use it in animation
p=plot(sf,0)
# To create the animation, I do a loop over all t's present in tvec
anim = @animate for i ∈ 0:length(tvec)-1
push!(p,tvec(i),sf(i))
frame(anim)
end
# And finally I (try to) display the result
gif(anim,"scale_factor.gif",fps=30)
【问题讨论】:
标签: julia