aboutsummaryrefslogtreecommitdiff
path: root/hw8/TimeDependentSchrodingerEquation.jl
diff options
context:
space:
mode:
authorsotech117 <michael_foiani@brown.edu>2024-05-07 07:00:43 -0400
committersotech117 <michael_foiani@brown.edu>2024-05-07 07:00:43 -0400
commitbc515d3acdd94847b6e7aa6135bc234b46161db6 (patch)
tree3184e9797e93b238e672442aea56b210ba5e5751 /hw8/TimeDependentSchrodingerEquation.jl
parent95eb65429d24a897307601415c716e9042033982 (diff)
add hw9 and hw8
Diffstat (limited to 'hw8/TimeDependentSchrodingerEquation.jl')
-rw-r--r--hw8/TimeDependentSchrodingerEquation.jl38
1 files changed, 19 insertions, 19 deletions
diff --git a/hw8/TimeDependentSchrodingerEquation.jl b/hw8/TimeDependentSchrodingerEquation.jl
index dacbced..1d4f315 100644
--- a/hw8/TimeDependentSchrodingerEquation.jl
+++ b/hw8/TimeDependentSchrodingerEquation.jl
@@ -7,26 +7,26 @@ using DifferentialEquations
# useful functions:
function H(psi, dx) # action of Hamiltonian on wavefunction
- Hpsi = zeros(ComplexF64, size(psi))
- # -(1/2) * laplacian(psi) (m = hbar = 1)
- Hpsi[2:end-1] = 0.5 * (2.0*psi[2:end-1] - psi[3:end] - psi[1:end-2])/(dx*dx)
-
- # periodic boundary conditions
- #Hpsi[1] = 0.5 * (2.0*psi[1] - psi[2] - psi[end])/(dx*dx)
- #Hpsi[end] = 0.5 * (2.0*psi[end] - psi[end-1] - psi[1])/(dx*dx)
- return Hpsi
+ Hpsi = zeros(ComplexF64, size(psi))
+ # -(1/2) * laplacian(psi) (m = hbar = 1)
+ Hpsi[2:end-1] = 0.5 * (2.0 * psi[2:end-1] - psi[3:end] - psi[1:end-2]) / (dx * dx)
+
+ # periodic boundary conditions
+ #Hpsi[1] = 0.5 * (2.0*psi[1] - psi[2] - psi[end])/(dx*dx)
+ #Hpsi[end] = 0.5 * (2.0*psi[end] - psi[end-1] - psi[1])/(dx*dx)
+ return Hpsi
end
derivative(psi, dx) = -1.0im * H(psi, dx)
function initialWavefunction(x::Vector{Float64}, x0 = 10.0, Delta = 1.0, k = 4.0)
- Delta2 = Delta^2
- return exp.(- (x .- x0).^2 / Delta2 ) .* exp.(1.0im * k * x)
+ Delta2 = Delta^2
+ return exp.(-(x .- x0) .^ 2 / Delta2) .* exp.(1.0im * k * x)
end
function normalization(psi, dx) # normalization of wavefunction
- n2 = dot(psi, psi) * dx
- return sqrt(n2)
+ n2 = dot(psi, psi) * dx
+ return sqrt(n2)
end
prob(psi) = real(psi .* conj(psi))
@@ -56,17 +56,17 @@ dt = 0.0001
tspan = (0.0, tf)
function timeEvolve(psi0, tf, dt) # second order Runge-Kutta algorithm
- psi = psi0
- for t in range(0, stop=tf, step=dt)
- psiMid = psi + 0.5 * dt * derivative(psi, dx)
- psi = psi + dt * derivative(psiMid, dx)
- end
- return psi
+ psi = psi0
+ for t in range(0, stop = tf, step = dt)
+ psiMid = psi + 0.5 * dt * derivative(psi, dx)
+ psi = psi + dt * derivative(psiMid, dx)
+ end
+ return psi
end
tendency(psi, dx, t) = derivative(psi, dx) # use ODE solver
problem = ODEProblem(tendency, psi0, tspan, dx) # specify ODE
-sol = solve(problem, Tsit5(), reltol=1e-12, abstol=1e-12) # solve using Tsit5 algorithm to specified accuracy
+sol = solve(problem, Tsit5(), reltol = 1e-12, abstol = 1e-12) # solve using Tsit5 algorithm to specified accuracy
# compare initial and final wavefunction probabilities
#psi = timeEvolve(psi0, tf, dt)