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 /examples/FallingBall3.jl | |
parent | adb65f3f12061e6cc8919338d28e006f7fa01c2f (diff) |
pull examples and complete homework 1
Diffstat (limited to 'examples/FallingBall3.jl')
-rw-r--r-- | examples/FallingBall3.jl | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/examples/FallingBall3.jl b/examples/FallingBall3.jl new file mode 100644 index 0000000..123a19a --- /dev/null +++ b/examples/FallingBall3.jl @@ -0,0 +1,34 @@ +#!/Applications/Julia-1.8.app/Contents/Resources/julia/bin/julia + +using Plots # for plotting trajectory +using DifferentialEquations # for solving ODEs + +g = 9.8 # acceleration of gravity in m/s^2 + +t_final = 1.0 # final time of trajectory +p = 0.0 # parameters (not used here) + +function tendency!(dyv::Vector{Float64}, yv::Vector{Float64}, p, t::Float64) # ! notation tells us that arguments will be modified + y = yv[1] # 2D phase space; use vcat(x, v) to combine 2 vectors + v = yv[2] # dy/dt = v + a = -g # dv/dt = -g + + dyv[1] = v + dyv[2] = a +end + +y0 = 10.0 # initial position in meters +v0 = 0.0 # initial velocity in m/s +yv0 = [y0, v0] # initial condition in phase space +tspan = (0.0, t_final) # span of time to simulate + +prob = ODEProblem(tendency!, yv0, tspan, p) # specify ODE +sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8) # solve using Tsit5 algorithm to specified accuracy + +println("\n\t Results") +println("final time = ", sol.t[end]) +println("y = ", sol[1, end], " and v = ", sol[2, end]) +println("exact v = ", v0 - g * t_final) +println("exact y = ", y0 + v0 * t_final - 0.5 * g * t_final^2.0) + +plot(sol, idxs = (1)) # plot position as a function of time |