aboutsummaryrefslogtreecommitdiff
path: root/hw1/1-3.jl
diff options
context:
space:
mode:
Diffstat (limited to 'hw1/1-3.jl')
-rw-r--r--hw1/1-3.jl40
1 files changed, 40 insertions, 0 deletions
diff --git a/hw1/1-3.jl b/hw1/1-3.jl
new file mode 100644
index 0000000..ad3f1b0
--- /dev/null
+++ b/hw1/1-3.jl
@@ -0,0 +1,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)
+