xxxxxxxxxx
4
1
begin
2
using Plots
3
using SparseArrays
4
end
initCond (generic function with 1 method)
xxxxxxxxxx
11
1
function initCond(xgrid, sType)
2
3
if(sType == 1)
4
y = 1/(sqrt(2*pi)) * exp.(xgrid .^ 2 ./ (-2)) #gaussian pluck in center
5
6
elseif(sType == 2)
7
y = 0.25*sin.(3*xgrid * (pi/xgrid[end-1])) #standing sinusoidal wave
8
end
9
10
return (copy(y), copy(y))
11
end
Wave Equation
Finite Difference
Let
Matrix form
Single string
n :: Number of points in discrete spatial grid
xi :: Initial co-ordinate value in grid
xf :: Final co-ordinate value in grid
m :: Number of timesteps
dt :: Size of each time step
c :: Speed of wave in string
waveEquation (generic function with 1 method)
xxxxxxxxxx
32
1
function waveEquation(n::Int64, xi::Float64, xf::Float64, m::Int64, dt::Float64, c::Float64)
2
y = zeros(Float64, (n, m)) #height of string at each point in space (n) and time (m)
3
4
dx = (xf-xi)/n #spacing of discrete grid
5
xgrid = collect(xi:dx:(xf-dx)) #discretized spatial grid
6
7
#CREATING EVOLUTION MATRIX
8
α = (c*dt/dx)^2
9
M = SparseArrays.spdiagm(-1 => α*ones(n-1), 0 => 2(1-α)*ones(n), 1 => α*ones(n-1))
10
11
#INITIAL CONDITIONS
12
y[:, 1], y[:, 2] = initCond(xgrid, 1)
13
14
#TIME EVOLUTION LOOP
15
for j in 2:(m-1)
16
y[:, j+1] = M * y[:, j] - y[:, j-1]
17
18
#SOURCE TERMS
19
#y[1, j+1] = 0.1*sin(5*j*dt) #sinusoidal driving source
20
#y[1, j+1] = 0.125 * exp((j*dt - 2)^2/ (-2*0.1)) #single gaussian pulse
21
22
#BOUNDARY CONDITION
23
#y[n, j+1] = y[n-1, j+1] #open end
24
end
25
26
#GIF CREATION
27
anim = for j in 1:5:m
28
plot(xgrid, y[:, j], ylim = (-0.3, 0.3))
29
end
30
31
return anim
32
end
This code was written hastily, so it does not elegantly accomodate initial/boundary conditions and source terms.
You can however uncomment the specific lines above and run the function call below manually to switch between them.
xxxxxxxxxx
1
1
waveEquation(200, -10.0, 10.0, 1000, 0.01, 3.0)
Composite string
n :: Number of points in discrete spatial grid
xi :: Initial co-ordinate value in grid
xf :: Final co-ordinate value in grid
m :: Number of timesteps
dt :: Size of each time step
c1 :: Speed of wave in string 1
c2 :: Speed of wave in string 2
waveEquationComposite (generic function with 1 method)
xxxxxxxxxx
40
1
function waveEquationComposite(n::Int64, xi::Float64, xf::Float64, m::Int64, dt::Float64, c1::Float64, c2::Float64)
2
y = zeros(Float64, (n, m)) #height of string at each point in space (n) and time (m)
3
4
dx = (xf-xi)/n #spacing of discrete grid
5
xgrid = collect(xi:dx:(xf-dx)) #discretized spatial grid
6
7
#CREATING EVOLUTION MATRIX
8
α = (c1*dt/dx)^2
9
β = (c2*dt/dx)^2
10
11
nmid = n÷2
12
M1 = SparseArrays.spdiagm(-1 => α*ones(nmid-1), 0 => 2(1-α)*ones(nmid), 1 => α*ones(nmid-1))
13
M2 = SparseArrays.spdiagm(-1 => β*ones(nmid-1), 0 => 2(1-β)*ones(nmid), 1 => β*ones(nmid-1))
14
M = SparseArrays.blockdiag(M1, M2)
15
M[nmid, nmid + 1] = α
16
M[nmid + 1, nmid] = β
17
18
#INITIAL CONDITIONS
19
#y[:, 1], y[:, 2] = initCond(xgrid, 1)
20
21
#TIME EVOLUTION LOOP
22
for j in 2:(m-1)
23
y[:, j+1] = M * y[:, j] - y[:, j-1]
24
25
#SOURCE TERMS
26
#y[1, j+1] = 0.1*sin(5*j*dt) #sinusoidal driving source
27
y[1, j+1] = 0.125 * exp((j*dt - 2)^2/ (-2*0.1)) #single gaussian pulse
28
29
#BOUNDARY CONDITION
30
#y[n, j+1] = y[n-1, j+1] #open end
31
end
32
33
#GIF CREATION
34
anim = for j in 1:5:m
35
plot(xgrid[1:nmid], y[1:nmid, j], ylim = (-0.3, 0.3))
36
plot!(xgrid[nmid:end], y[nmid:end, j], ylim = (-0.3, 0.3), lw = 2)
37
end
38
39
return anim
40
end
xxxxxxxxxx
1
1
waveEquationComposite(200, -10.0, 10.0, 1000, 0.01, 3.0, 5.0)