aboutsummaryrefslogtreecommitdiff
path: root/examples/FallingBall.jl
blob: 35d4b315a185a3fc25326d6b3573c9c26163fc6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/Applications/Julia-1.8.app/Contents/Resources/julia/bin/julia

dt = 0.01 # time step in seconds
g = 9.8 # acceleration of gravity in m/s^2

function dynamics(y::Float64, v::Float64, t::Float64) 
   for i in 1:100
      y = y + v * dt
      v = v - g * dt
      t = t + dt
   end

   return y, v, t
end

y0 = 10.0 # initial position in meters
v0 = 0.0 # initial velocity in m/s

y, v, t = dynamics(y0, v0, 0.0) # evolave for 100 time steps

println("\n\t Results")
println("final time  = ", t)
println("y = ", y, " and v = ", v)
println("exact v = ", v0 - g * t)
println("exact y = ", y0 + v0 * t - 0.5 * g * t^2.0)