aboutsummaryrefslogtreecommitdiff
path: root/hw1/1-3.jl
blob: ad3f1b0244fcb44613b1876ca2bd9bf17b948713 (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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using Plots # for plotting trajectory
using DifferentialEquations # for solving ODEs


# INITIAL CONDITIONALS AND PARAMETERS
a = 10.0 # 1st drag coefficient
b = 1.0 # 2nd drag coefficient (on v)
v0 = 0.0 # initial velocity in m/s
t_final = 10.0 # time of trajectory in s


# EULER'S METHOD
dt = 0.01 # time step
steps = Int64(t_final/dt) # number of time steps

v = zeros(steps+1) # initial array of velocities
t = zeros(steps+1) # initial array of time intervals

function dynamics!(v::Vector{Float64}, t::Vector{Float64}) 
    for i in 1:steps
        # equation: dv = dt(a - b*v)
        dv = dt*(a - b*v[i])

        v[i+1] = v[i] + dv
        t[i+1] = t[i] + dt
    end
end

# do the calculation, store into arrays
v[1] = v0
t[1] = 0.0
dynamics!(v, t)

# print the parameters & terminal velocity
println("Parameters (a, b, v0, t_final): ", a, ", ", b, ", ", v0, ", ", t_final)
println("Terminal velocity: ", v[end])

# plot the velocities over time
plot(t, v, xlabel="time (s)", ylabel="velocity (m/s)", title="Velocity vs. time (affected by drag, w/ a,b=10,3)", label="", lw=2, color=:blue)