xxxxxxxxxx4
1
begin2
using Plots3
using SparseArrays4
endinitCond (generic function with 1 method)xxxxxxxxxx11
1
function initCond(xgrid, sType)2
3
if(sType == 1)4
y = 1/(sqrt(2*pi)) * exp.(xgrid .^ 2 ./ (-2)) #gaussian pluck in center5
6
elseif(sType == 2) 7
y = 0.25*sin.(3*xgrid * (pi/xgrid[end-1])) #standing sinusoidal wave8
end 9
10
return (copy(y), copy(y))11
endWave 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)xxxxxxxxxx32
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 grid5
xgrid = collect(xi:dx:(xf-dx)) #discretized spatial grid6
7
#CREATING EVOLUTION MATRIX8
α = (c*dt/dx)^2 9
M = SparseArrays.spdiagm(-1 => α*ones(n-1), 0 => 2(1-α)*ones(n), 1 => α*ones(n-1))10
11
#INITIAL CONDITIONS12
y[:, 1], y[:, 2] = initCond(xgrid, 1)13
14
#TIME EVOLUTION LOOP15
for j in 2:(m-1)16
y[:, j+1] = M * y[:, j] - y[:, j-1] 17
18
#SOURCE TERMS19
#y[1, j+1] = 0.1*sin(5*j*dt) #sinusoidal driving source20
#y[1, j+1] = 0.125 * exp((j*dt - 2)^2/ (-2*0.1)) #single gaussian pulse21
22
#BOUNDARY CONDITION23
#y[n, j+1] = y[n-1, j+1] #open end24
end25
26
#GIF CREATION27
anim = for j in 1:5:m28
plot(xgrid, y[:, j], ylim = (-0.3, 0.3)) 29
end30
31
return anim32
endThis 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.
xxxxxxxxxx1
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)xxxxxxxxxx40
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 grid5
xgrid = collect(xi:dx:(xf-dx)) #discretized spatial grid6
7
#CREATING EVOLUTION MATRIX8
α = (c1*dt/dx)^29
β = (c2*dt/dx)^210
11
nmid = n÷212
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 CONDITIONS19
#y[:, 1], y[:, 2] = initCond(xgrid, 1)20
21
#TIME EVOLUTION LOOP22
for j in 2:(m-1)23
y[:, j+1] = M * y[:, j] - y[:, j-1] 24
25
#SOURCE TERMS26
#y[1, j+1] = 0.1*sin(5*j*dt) #sinusoidal driving source27
y[1, j+1] = 0.125 * exp((j*dt - 2)^2/ (-2*0.1)) #single gaussian pulse28
29
#BOUNDARY CONDITION30
#y[n, j+1] = y[n-1, j+1] #open end31
end32
33
#GIF CREATION34
anim = for j in 1:5:m35
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
end38
39
return anim40
endxxxxxxxxxx1
1
waveEquationComposite(200, -10.0, 10.0, 1000, 0.01, 3.0, 5.0)