Skip to content

Commit 91e2da4

Browse files
committed
Lecture 12: Scripts
1 parent c07c8f8 commit 91e2da4

File tree

4 files changed

+569
-0
lines changed

4 files changed

+569
-0
lines changed

scripts/lecture_12/Project.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
[deps]
2+
DifferentialEquations = "0c46a032-eb83-5123-abaf-570d42b7fbaa"
3+
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
4+
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
5+
6+
[compat]
7+
DifferentialEquations = "= 6.16.0"
8+
Plots = "= 1.10.3"
9+
julia = "1.5"

scripts/lecture_12/script.jl

Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
using DifferentialEquations
2+
using LinearAlgebra
3+
using Plots
4+
5+
struct Wave
6+
f
7+
g
8+
c
9+
end
10+
11+
# Exercise
12+
13+
14+
15+
# Visualization
16+
17+
function plot_wave(y, file_name; fps = 60, kwargs...)
18+
anim = @animate for (i, y_row) in enumerate(eachrow(y))
19+
plot(
20+
y_row;
21+
title = "t = $(i-1)Δt",
22+
xlabel = "x",
23+
ylabel = "y(t, x)",
24+
legend = false,
25+
linewidth = 2,
26+
kwargs...
27+
)
28+
end
29+
gif(anim, file_name; fps, show_msg = false)
30+
31+
return nothing
32+
end
33+
34+
# Exercise
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
46+
47+
48+
# DiferentialEquations
49+
50+
f(u,p,t) = 0.98*u
51+
52+
u0 = 1.0
53+
tspan = (0.0, 1.0)
54+
55+
prob = ODEProblem(f, u0, tspan)
56+
57+
sol = solve(prob)
58+
59+
plot(sol; label="")
60+
61+
sol(0.8)
62+
63+
# Exercise
64+
65+
66+
67+
# Lorenz system
68+
69+
function lorenz(u, p, t)
70+
σ, ρ, β = p
71+
x_t = σ*(u[2]-u[1])
72+
y_t = u[1]*-u[3]) - u[2]
73+
z_t = u[1]*u[2] - β*u[3]
74+
return [x_t; y_t; z_t]
75+
end
76+
77+
u0 = [1.0; 0.0; 0.0]
78+
p = [10; 28; 8/3]
79+
80+
tspan = (0.0, 100.0)
81+
prob = ODEProblem(lorenz, u0, tspan, p)
82+
83+
sol = solve(prob)
84+
85+
plot(sol)
86+
87+
plt1 = plot(sol, vars=(1,2,3), label="")
88+
89+
plot(sol, vars=(1,2,3), denseplot=false; label="")
90+
91+
traj = hcat(sol.u...)
92+
plot(traj[1,:], traj[2,:], traj[3,:]; label="")
93+
94+
# Exercise
95+
96+
97+
98+
# Solution comparison
99+
100+
hcat(sol(tspan[2]), sol0(tspan[2]))
101+
102+
103+
104+
105+
106+
107+
108+
109+
110+
111+
112+
# Bisection setup
113+
114+
function bisection(f, a, b; tol=1e-6)
115+
fa = f(a)
116+
fb = f(b)
117+
fa == 0 && return a
118+
fb == 0 && return b
119+
fa*fb > 0 && error("Wrong initial values for bisection")
120+
while b-a > tol
121+
c = (a+b)/2
122+
fc = f(c)
123+
fc == 0 && return c
124+
if fa*fc > 0
125+
a = c
126+
fa = fc
127+
else
128+
b = c
129+
fb = fc
130+
end
131+
end
132+
return (a+b)/2
133+
end
134+
135+
# PMSM structure
136+
137+
struct PMSM{T<:Real}
138+
ρ::T
139+
ω::T
140+
A::Matrix{T}
141+
invA::Matrix{T}
142+
143+
function PMSM(ρ, ω)
144+
A = -ρ*[1 0; 0 1] -ω*[0 -1; 1 0]
145+
return new{eltype(A)}(ρ, ω, A, inv(A))
146+
end
147+
end
148+
149+
function expA(p::PMSM, t)
150+
ρ, ω = p.ρ, p.ω
151+
return exp(-ρ*t)*[cos*t) sin*t); -sin*t) cos*t)]
152+
end
153+
154+
ρ = 0.1
155+
ω = 2
156+
x0 = [0;-0.5]
157+
q = [1;0]
158+
159+
ps = PMSM(ρ, ω)
160+
161+
# Exercise
162+
163+
164+
165+
# Exercise
166+
167+
168+
169+
# Plotting trajectory
170+
171+
ts = 0:0.0001:10
172+
173+
xs1 = trajectory_fin_diff(ps, x0, ts, q)
174+
xs2 = trajectory_exact(ps, x0, ts, q)
175+
176+
plot(xs1[1,:], xs1[2,:], label="Finite differences")
177+
plot!(xs2[1,:], xs2[2,:], label="True value")
178+
179+
# Exercise
180+
181+
182+
183+
# Plotting trajectory
184+
185+
function trajectory_control(p::PMSM, x0, ts, q, U_max, p0)
186+
xs = zeros(length(x0), length(ts))
187+
188+
for (i, t) in enumerate(ts)
189+
eAt = expA(p, t)
190+
emAt = expA(p, -t)
191+
xs[:, i] = eAt*(x0 + p.invA * (I - emAt)*q + U_max/ρ*(exp*t) - 1)*p0)
192+
end
193+
return xs
194+
end
195+
196+
p0 = ps.ρ/(U_max*(exp(ps.ρ*τ)-1))*(expA(ps, -τ)*x_t - x0 - ps.invA*(I-expA(ps, -τ))*q)
197+
p0 /= norm(p0)
198+
199+
ts = range(0, τ; length=100)
200+
201+
traj = trajectory_control(ps, x0, ts, q, U_max, p0)
202+
203+
plot(traj[1,:], traj[2,:], label="Optimal trajectory")
204+
scatter!([x0[1]], [x0[2]], label="Starting point")
205+
scatter!([x_t[1]], [x_t[2]], label="Target point")
206+
207+
# BONUS
208+
209+
ts = 0:0.01:10
210+
211+
plt = plot()
212+
for α = 0:π/4:2*π
213+
trj = trajectory_control(ps, x0, ts, q, U_max, [sin(α); cos(α)])
214+
plot!(plt, trj[1,:], trj[2,:], label="")
215+
end
216+
display(plt)
217+
218+
219+
220+

scripts/lecture_12/script_init.jl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using Pkg
2+
Pkg.activate(".")
3+
Pkg.instantiate()
4+
5+
using DifferentialEquations
6+
using LinearAlgebra
7+
using Plots
8+
9+
f(u,p,t) = 0.98*u
10+
11+
u0 = 1.0
12+
tspan = (0.0, 1.0)
13+
14+
prob = ODEProblem(f, u0, tspan)
15+
16+
sol = solve(prob)
17+
18+
plot(sol; label="")
19+
20+
sol(0.8)
21+
22+
function lorenz(u, p, t)
23+
σ, ρ, β = p
24+
x_t = σ*(u[2]-u[1])
25+
y_t = u[1]*-u[3]) - u[2]
26+
z_t = u[1]*u[2] - β*u[3]
27+
return [x_t; y_t; z_t]
28+
end
29+
30+
u0 = [1.0; 0.0; 0.0]
31+
p = [10; 28; 8/3]
32+
33+
tspan = (0.0, 100.0)
34+
prob = ODEProblem(lorenz, u0, tspan, p)

0 commit comments

Comments
 (0)