begin
using Pkg ;
using PlutoUI , Plots
end
mutable struct Pendulum
θ :: Float64 # in radians
ω :: Float64 # in rad/sec
l :: Float64
mass :: Float64
radius :: Float64
anchor :: Vector{Float64}
traj :: Vector{Float64}
end
get_alpha (generic function with 2 methods)
get_alpha(obj::Pendulum, g = -9.8) = (g/obj.l) * sin(obj.θ)
update! (generic function with 1 method)
function update!(obj::Pendulum, Δt::Float64)
obj.ω += Δt * get_alpha(obj )
obj.θ += Δt * obj .ω # Forward Euler method
push!(obj .traj, obj .θ)
end
evolve! (generic function with 1 method)
function evolve!(obj::Pendulum, Δt::Float64, num_iter::Int64)
for time in 1:num_iter
update!(obj , Δt )
end
end
get_pos (generic function with 1 method)
function get_pos(obj::Pendulum, n::Int64)
return [
]
end
circle (generic function with 1 method)
function circle(centre::Vector{Float64}, r::Float64)
theta = range(start = 0., stop = 2π, length = 50)
xcoords = centre [1] .+ r .* cos.(theta )
ycoords = centre [2] .+ r .* sin.(theta )
return xcoords , ycoords
end
display (generic function with 1 method)
function display(obj::Pendulum, n::Int64, lims::Vector{Float64})
x, y = get_pos(obj , n )
# string
plot(
[obj .anchor[1], x ],
[obj .anchor[2], y ],
lw = 2,
lc = :black)
# bob
plot!(
circle([x , y ],
lw = 2,
lc = :black,
st = :shape,
c = :white)
# anchor
plot!(
circle(obj .anchor, obj .radius/8),
lw = 2,
lc = :black,
st = :shape,
c = :black)
# global canvas settings
plot!(
aspect_ratio = 1,
legend = false,
axis = nothing,
framestyle = :box,
xlims = lims [1:2],
ylims = lims [3:4]
)
begin
dt = 0.005
nsteps = 1000
object = Pendulum(π/180 * 45., 0., 1., 1., 0.1, [0., 1.], [π/180 * 45.])
evolve!(object , dt , nsteps )
end
# display(object, frame_number, [-1.01, 1.01, -0.1, 1.1])
@bind frame_number Slider(1:2:nsteps)
@gif for frame in 1:3:nsteps
display(object, frame , [-1.01, 1.01, -0.1, 1.1])
end
Saved animation to
"C:\\Users\\saksh\\AppData\\Local\\Temp\\jl_rY1KqbVGo6.gif"