From 3c7d70ebd43423220b266dab218ca6d687996d08 Mon Sep 17 00:00:00 2001 From: sotech117 Date: Thu, 1 Feb 2024 12:35:05 -0500 Subject: pull examples and complete homework 1 --- examples/FallingBall.jl | 25 +++++++++++++++++++++++++ examples/FallingBall2.jl | 44 ++++++++++++++++++++++++++++++++++++++++++++ examples/FallingBall3.jl | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 examples/FallingBall.jl create mode 100644 examples/FallingBall2.jl create mode 100644 examples/FallingBall3.jl (limited to 'examples') diff --git a/examples/FallingBall.jl b/examples/FallingBall.jl new file mode 100644 index 0000000..35d4b31 --- /dev/null +++ b/examples/FallingBall.jl @@ -0,0 +1,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) \ No newline at end of file diff --git a/examples/FallingBall2.jl b/examples/FallingBall2.jl new file mode 100644 index 0000000..535af6d --- /dev/null +++ b/examples/FallingBall2.jl @@ -0,0 +1,44 @@ +#!/Applications/Julia-1.8.app/Contents/Resources/julia/bin/julia + +using Plots # for plotting trajectory + +g = 9.8 # acceleration of gravity in m/s^2 + +dt = 0.01 # time step in seconds +t_final = 1.0 # final time of trajectory + +steps = Int64(t_final/dt) # number of time steps + +y = zeros(steps+1) # initial array of heights in meters +v = zeros(steps+1) # initial array of velocities in m/s + +function dynamics!(y, v, t::Float64) # ! notation tells us that arguments will be modified + for i in 1:steps + y[i+1] = y[i] + v[i] * dt + v[i+1] = v[i] - g * dt + #y[i+1] = y[i] + 0.5 * (v[i] + v[i+1]) * dt + t = t + dt + end + + return t +end + +y0 = 10.0 # initial position in meters +v0 = 0.0 # initial velocity in m/s + +y[1] = y0 +v[1] = v0 +t = dynamics!(y, v, 0.0) # evolve forward in time + +println("\n\t Results") +println("final time = ", t) +println("y = ", y[steps+1], " and v = ", v[steps+1]) +println("exact v = ", v0 - g * t) +println("exact y = ", y0 + v0 * t - 0.5 * g * t^2.0) + +plot(y) # plot position as a function of time + +# energy = g * y + 0.5 * v .* v # here the mass = 1 +# println("initial energy = ", energy[1]) +# println("final energy = ", energy[steps+1]) +# plot(energy) \ No newline at end of file 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 -- cgit v1.2.3-70-g09d2