Skip to content

Commit fa75fe2

Browse files
committed
Add weaved files
1 parent f39501f commit fa75fe2

18 files changed

+831
-131
lines changed

html/models/01-classical_physics.html

+165-100
Large diffs are not rendered by default.

markdown/models/01-classical_physics.md

+589
Large diffs are not rendered by default.
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading
Loading

notebook/models/01-classical_physics.ipynb

+32-16
Large diffs are not rendered by default.

pdf/models/01-classical_physics.pdf

50.3 KB
Binary file not shown.

script/models/01-classical_physics.jl

+45-15
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,35 @@ plot(sol,linewidth=2,title ="Carbon-14 half-life", xaxis = "Time in thousands of
2121
plot!(sol.t, t->exp(-C₁*t),lw=3,ls=:dash,label="Analytical Solution")
2222

2323

24+
# Simple Harmonic Oscillator Problem
25+
using OrdinaryDiffEq, Plots
26+
27+
#Parameters
28+
ω = 1
29+
30+
#Initial Conditions
31+
x₀ = [0.0]
32+
dx₀ =/2]
33+
tspan = (0.0, 2π)
34+
35+
ϕ = atan((dx₀[1]/ω)/x₀[1])
36+
A = (x₀[1]^2 + dx₀[1]^2)
37+
38+
#Define the problem
39+
function harmonicoscillator(ddu,du,u,ω,t)
40+
ddu .= -ω^2 * u
41+
end
42+
43+
#Pass to solvers
44+
prob = SecondOrderODEProblem(harmonicoscillator, dx₀, x₀, tspan, ω)
45+
sol = solve(prob, DPRKN6())
46+
47+
#Plot
48+
plot(sol, vars=[2,1], linewidth=2, title ="Simple Harmonic Oscillator", xaxis = "Time", yaxis = "Elongation", label = ["x" "dx"])
49+
plot!(t->A*cos*t-ϕ), lw=3, ls=:dash, label="Analytical Solution x")
50+
plot!(t->-A*ω*sin*t-ϕ), lw=3, ls=:dash, label="Analytical Solution dx")
51+
52+
2453
# Simple Pendulum Problem
2554
using OrdinaryDiffEq, Plots
2655

@@ -34,18 +63,18 @@ tspan = (0.0,6.3)
3463

3564
#Define the problem
3665
function simplependulum(du,u,p,t)
37-
θ = u[1]
66+
θ = u[1]
3867
= u[2]
3968
du[1] =
4069
du[2] = -(g/L)*sin(θ)
4170
end
4271

4372
#Pass to solvers
44-
prob = ODEProblem(simplependulum,u₀, tspan)
73+
prob = ODEProblem(simplependulum, u₀, tspan)
4574
sol = solve(prob,Tsit5())
4675

4776
#Plot
48-
plot(sol,linewidth=2,title ="Simple Pendulum Problem", xaxis = "Time", yaxis = "Height", label = ["Theta","dTheta"])
77+
plot(sol,linewidth=2,title ="Simple Pendulum Problem", xaxis = "Time", yaxis = "Height", label = ["\\theta" "d\\theta"])
4978

5079

5180
p = plot(sol,vars = (1,2), xlims = (-9,9), title = "Phase Space Plot", xaxis = "Velocity", yaxis = "Position", leg=false)
@@ -104,7 +133,7 @@ plot(ps...)
104133
#Constants and setup
105134
using OrdinaryDiffEq
106135
initial2 = [0.01, 0.005, 0.01, 0.01]
107-
tspan2 = (0.,200.)
136+
tspan2 = (0.,500.)
108137

109138
#Define the problem
110139
function double_pendulum_hamiltonian(udot,u,p,t)
@@ -127,20 +156,21 @@ cb = ContinuousCallback(condition,affect!,nothing,
127156

128157
# Construct Problem
129158
poincare = ODEProblem(double_pendulum_hamiltonian, initial2, tspan2)
130-
sol2 = solve(poincare, Vern9(), save_everystep = false, callback=cb, abstol=1e-9)
159+
sol2 = solve(poincare, Vern9(), save_everystep = false, save_start=false, save_end=false, callback=cb, abstol=1e-16, reltol=1e-16,)
131160

132161
function poincare_map(prob, u₀, p; callback=cb)
133-
_prob = ODEProblem(prob.f,[0.01, 0.01, 0.01, u₀],prob.tspan)
134-
sol = solve(_prob, Vern9(), save_everystep = false, callback=cb, abstol=1e-9)
135-
scatter!(p, sol, vars=(3,4), markersize = 2)
162+
_prob = ODEProblem(prob.f, u₀, prob.tspan)
163+
sol = solve(_prob, Vern9(), save_everystep = false, save_start=false, save_end=false, callback=cb, abstol=1e-16, reltol=1e-16)
164+
scatter!(p, sol, vars=(3,4), markersize = 3, msw=0)
136165
end
137166

138167

139-
p = scatter(sol2, vars=(3,4), leg=false, markersize = 2, ylims=(-0.01,0.03))
140-
for i in -0.01:0.00125:0.01
141-
poincare_map(poincare, i, p)
168+
lβrange = -0.02:0.0025:0.02
169+
p = scatter(sol2, vars=(3,4), leg=false, markersize = 3, msw=0)
170+
forin lβrange
171+
poincare_map(poincare, [0.01, 0.01, 0.01, lβ], p)
142172
end
143-
plot(p,ylims=(-0.01,0.03))
173+
plot(p, xlabel="\\beta", ylabel="l_\\beta", ylims=(0, 0.03))
144174

145175

146176
using OrdinaryDiffEq, Plots
@@ -191,7 +221,7 @@ energy = map(x->E(x...), sol.u)
191221
@show ΔE = energy[1]-energy[end]
192222

193223
#Plot
194-
plot(sol.t, energy, title = "Change in Energy over Time", xaxis = "Time in iterations", yaxis = "Change in Energy")
224+
plot(sol.t, energy .- energy[1], title = "Change in Energy over Time", xaxis = "Time in iterations", yaxis = "Change in Energy")
195225

196226

197227
function HH_acceleration!(dv,v,u,p,t)
@@ -219,14 +249,14 @@ energy = map(x->E(x[3], x[4], x[1], x[2]), sol2.u)
219249
@show ΔE = energy[1]-energy[end]
220250

221251
#Plot
222-
plot(sol2.t, energy, title = "Change in Energy over Time", xaxis = "Time in iterations", yaxis = "Change in Energy")
252+
plot(sol2.t, energy .- energy[1], title = "Change in Energy over Time", xaxis = "Time in iterations", yaxis = "Change in Energy")
223253

224254

225255
sol3 = solve(prob, DPRKN6());
226256
energy = map(x->E(x[3], x[4], x[1], x[2]), sol3.u)
227257
@show ΔE = energy[1]-energy[end]
228258
gr()
229-
plot(sol3.t, energy, title = "Change in Energy over Time", xaxis = "Time in iterations", yaxis = "Change in Energy")
259+
plot(sol3.t, energy .- energy[1], title = "Change in Energy over Time", xaxis = "Time in iterations", yaxis = "Change in Energy")
230260

231261

232262
using DiffEqTutorials

0 commit comments

Comments
 (0)