diff options
author | sotech117 <michael_foiani@brown.edu> | 2024-02-01 12:35:05 -0500 |
---|---|---|
committer | sotech117 <michael_foiani@brown.edu> | 2024-02-01 12:35:05 -0500 |
commit | 3c7d70ebd43423220b266dab218ca6d687996d08 (patch) | |
tree | aa4b9869ea4248858b8aee46f73da55abb22c665 /hw1/1-3.jl | |
parent | adb65f3f12061e6cc8919338d28e006f7fa01c2f (diff) |
pull examples and complete homework 1
Diffstat (limited to 'hw1/1-3.jl')
-rw-r--r-- | hw1/1-3.jl | 40 |
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) + |