@inline function friction_fn(v::Float64, vt::Float64)
if v > vt
return - v * 3
else
return - vt * 3 * sign(v)
end
end
function simulate_spring_mass_funky_damper(x0, T=10, dt=0.0001, vt=1.0)
times::Array{Float64, 1} = [t for t in range(0, T, step=dt)]
positions::Array{Float64, 1} = zeros(Float64, size(times))
v::Float64 = 0.
a::Float64 = 0.
x::Float64 = x0
@inbounds positions[1] = 1
for ii in 2:length(times)
@inbounds t = times[ii]
a = friction_fn(v, vt) - 100. *x
v = v + a*dt
x = x + v*dt
@inbounds positions[ii] = x/x0
end
return times, positions
end
simulate_spring_mass_funky_damper (generic function with 4 methods)
plot(simulate_spring_mass_funky_damper(0.1), label=0.1)
plot!(simulate_spring_mass_funky_damper(1), label=1)
plot!(simulate_spring_mass_funky_damper(10), label=10)
@benchmark simulate_spring_mass_funky_damper(1.)
BenchmarkTools.Trial: 2312 samples with 1 evaluation. Range (min … max): 1.277 ms … 17.018 ms ┊ GC (min … max): 0.00% … 81.52% Time (median): 2.137 ms ┊ GC (median): 0.00% Time (mean ± σ): 2.147 ms ± 923.427 μs ┊ GC (mean ± σ): 6.20% ± 11.78% ▇▄▂ ▁ ▇█▆▅▃▂▁ ▁ ████▇█▇▅▁███████▇▆█▅▅▄▅▄▃▁▃▃▁▁▁▁▁▄▁▁▁▁▁▁▃▁▁▁▁▃▁▅▃▅▆▅▇▆▆▅▆▁▆ █ 1.28 ms Histogram: log(frequency) by time 6.27 ms < Memory estimate: 1.53 MiB, allocs estimate: 4.