Skip to content

Commit bc176c2

Browse files
authored
Add harmonic oscillator examples (#879)
* Add harmonic oscillator examples * Add tunnelling eigenvalue example
1 parent 557aea9 commit bc176c2

File tree

6 files changed

+108
-2
lines changed

6 files changed

+108
-2
lines changed

examples/Eigenvalue.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,30 @@ include("Eigenvalue_standard.jl")
77

88
import Plots
99
using LinearAlgebra: norm
10-
p = Plots.plot(V; legend=false, ylim=(-Inf, λ[22]))
10+
p = Plots.plot(V, legend=false, ylim=(-Inf, λ[22]))
1111
for k=1:20
12-
Plots.plot!(real(v[k]/norm(v[k]) + λ[k]), )
12+
Plots.plot!(real(v[k]/norm(v[k]) + λ[k]))
1313
end
1414
p
1515

1616
# If the solutions are not relatively constant near the boundary then one should push
1717
# the boundaries further out.
1818

19+
include("Eigenvalue_tunnelling.jl")
20+
21+
# We plot the first few eigenfunctions
22+
p = Plots.plot(V, legend=false)
23+
Plots.vline!([-Lx/2, Lx/2], linecolor=:black)
24+
p_twin = Plots.twinx(p)
25+
for k=1:6
26+
Plots.plot!(p_twin, real(v[k]/norm(v[k]) + λ[k]), label="$k")
27+
end
28+
p
29+
30+
# Note that the parity symmetry isn't preserved exactly at finite matrix sizes.
31+
# In general, it's better to preserve the symmetry of the operator matrices (see section below),
32+
# and projecting them on to the appropriate subspaces.
33+
1934
# For problems with different contraints or boundary conditions,
2035
# `B` can be any zero functional constraint, e.g., `DefiniteIntegral()`.
2136

examples/Eigenvalue_standard.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
# ```
99
# where ``λ_i`` is the ``i^{th}`` eigenvalue of the ``L`` and has a corresponding
1010
# *eigenfunction* ``\mathop{u}_i(x)``.
11+
12+
### Quantum harmonic oscillator
13+
1114
# A classic eigenvalue problem is known as the quantum harmonic oscillator where
1215
# ```math
1316
# \mathop{L} = -\frac{1}{2}\frac{\mathop{d}^2}{\mathop{dx}^2} + \frac{1}{2} x^2,

examples/Eigenvalue_tunnelling.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# ### Tunnelling
2+
3+
# We solve the Schrodinger equation in an infinite square well in the domain `-Lx/2..Lx/2`, with a barrier at the center from `-d/2..d/2`
4+
# ```math
5+
# \mathop{L} = -\frac{1}{2}\frac{\mathop{d}^2}{\mathop{dx}^2} + V(x),
6+
# ```
7+
# where we choose a smoothed barrier potential ``V(x)``.
8+
9+
Lx = 4 # size of the domain
10+
d = 1 # size of the barrier
11+
Δ = 0.1 # smoothing scale of the barrier
12+
x = Fun(-Lx/2..Lx/2)
13+
V = 5 * (1 + tanh((x + d/2)/Δ)) * (1 - tanh((x - d/2)/Δ));
14+
H = -𝒟^2/2 + V;
15+
16+
S = space(x)
17+
B = Dirichlet(S);
18+
19+
λ, v = ApproxFun.eigs(B, H, 1000, tolerance=1E-8);

examples/ODE.jl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@ import Plots
1616
Plots.plot(u, xlabel="x", ylabel="u(x)", legend=false)
1717

1818

19+
# ## Initial value problem
20+
# ### Undamped harmonic oscillator
21+
# ```math
22+
# \frac{d^2 u}{dt^2} + 4 u = 0,
23+
# ```
24+
# in an interval `0..2pi`, subject to the initial conditions ``u(0)=0``
25+
# and ``u'(0)=2``.
26+
27+
include("ODE_IVP.jl")
28+
29+
# We plot the solution
30+
t = a:0.1:b
31+
Plots.plot(t, u.(t), xlabel="t", label="u(t)")
32+
Plots.plot!(t, sin.(2t), label="Analytical", seriestype=:scatter)
33+
34+
# ### Damped harmonic oscillator
35+
# ```math
36+
# \frac{d^2 y}{dt^2} + 2\zeta\omega_0\frac{dy}{dt} + \omega_0^2 y = 0,
37+
# ```
38+
# from ``t=0`` to ``t=T``, subject to the initial conditions ``y(0)=4``
39+
# and ``y'(0)=0``.
40+
41+
include("ODE_IVP2.jl")
42+
43+
# Plot the solution
44+
import Plots
45+
Plots.plot(y, xlabel="t", ylabel="y(t)", legend=false)
46+
1947
# ## Increasing Precision
2048

2149
# Solving differential equations with high precision types is available.

examples/ODE_IVP.jl

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using ApproxFun
2+
using LinearAlgebra
3+
4+
# Construct the domain
5+
a, b = 0, 2pi
6+
d = a..b;
7+
8+
# We construct the differential operator ``L = d^2/dx^2 + 4``:
9+
D = Derivative(d)
10+
L = D^2 + 4;
11+
12+
# We have not chosen the space explicitly, and the solve chooses it to be `Chebyshev(d)` internally
13+
14+
# We incorporate the initial conditions using `ivp`, which is a shortcut for evaluating the function
15+
# and it's derivative at the left boundary of the domain
16+
A = [L; ivp()];
17+
18+
# Initial conditions
19+
u0 = 0;
20+
dtu0 = 2;
21+
22+
# We solve the differential equation
23+
u = \(A, [0,u0,dtu0], tolerance=1e-6);

examples/ODE_IVP2.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Construct the domain
2+
T = 12
3+
d = 0..T;
4+
5+
Dt = Derivative(d);
6+
ζ = 0.2 # damping ratio
7+
ω0 = 2 # oscillation frequency
8+
L = Dt^2 + 2ζ * ω0 * Dt + ω0^2;
9+
10+
# initial conditions
11+
y0 = 4; # initial displacement
12+
dty0 = 3; # initial velocity
13+
14+
# The differential operator along with the initial condition evaluation operator
15+
A = [L; ivp()];
16+
17+
# We solve the differential equation
18+
y = \(A, [0,y0,dty0], tolerance=1e-6);

0 commit comments

Comments
 (0)