@@ -21,6 +21,35 @@ plot(sol,linewidth=2,title ="Carbon-14 half-life", xaxis = "Time in thousands of
21
21
plot! (sol. t, t-> exp (- C₁* t),lw= 3 ,ls= :dash ,label= " Analytical Solution" )
22
22
23
23
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
+
24
53
# Simple Pendulum Problem
25
54
using OrdinaryDiffEq, Plots
26
55
@@ -34,18 +63,18 @@ tspan = (0.0,6.3)
34
63
35
64
# Define the problem
36
65
function simplependulum (du,u,p,t)
37
- θ = u[1 ]
66
+ θ = u[1 ]
38
67
dθ = u[2 ]
39
68
du[1 ] = dθ
40
69
du[2 ] = - (g/ L)* sin (θ)
41
70
end
42
71
43
72
# Pass to solvers
44
- prob = ODEProblem (simplependulum,u₀, tspan)
73
+ prob = ODEProblem (simplependulum, u₀, tspan)
45
74
sol = solve (prob,Tsit5 ())
46
75
47
76
# 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 " ])
49
78
50
79
51
80
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...)
104
133
# Constants and setup
105
134
using OrdinaryDiffEq
106
135
initial2 = [0.01 , 0.005 , 0.01 , 0.01 ]
107
- tspan2 = (0. ,200 . )
136
+ tspan2 = (0. ,500 . )
108
137
109
138
# Define the problem
110
139
function double_pendulum_hamiltonian (udot,u,p,t)
@@ -127,20 +156,21 @@ cb = ContinuousCallback(condition,affect!,nothing,
127
156
128
157
# Construct Problem
129
158
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 , )
131
160
132
161
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 )
136
165
end
137
166
138
167
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
+ for lβ in lβrange
171
+ poincare_map (poincare, [0.01 , 0.01 , 0.01 , lβ], p)
142
172
end
143
- plot (p,ylims= (- 0.01 , 0.03 ))
173
+ plot (p, xlabel = " \\ beta " , ylabel = " l_ \\ beta " , ylims= (0 , 0.03 ))
144
174
145
175
146
176
using OrdinaryDiffEq, Plots
@@ -191,7 +221,7 @@ energy = map(x->E(x...), sol.u)
191
221
@show ΔE = energy[1 ]- energy[end ]
192
222
193
223
# 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" )
195
225
196
226
197
227
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)
219
249
@show ΔE = energy[1 ]- energy[end ]
220
250
221
251
# 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" )
223
253
224
254
225
255
sol3 = solve (prob, DPRKN6 ());
226
256
energy = map (x-> E (x[3 ], x[4 ], x[1 ], x[2 ]), sol3. u)
227
257
@show ΔE = energy[1 ]- energy[end ]
228
258
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" )
230
260
231
261
232
262
using DiffEqTutorials
0 commit comments