Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. """
  4. Animation of the Schrodinger equation for a particle in a 1D box
  5.  
  6. -∂²/∂x² ψ(x,t) = i ∂/∂t ψ(x,t) (natural units)
  7.  
  8. for a non-eigenstate initial state ψ, with boundary conditions
  9.  
  10. ψ(0,t) = ψ(1,t) = 0.
  11.  
  12. The solution to the above differential equation is found by separation of
  13. variables, and is as follows:
  14.  
  15. ψ(x,t) = ∑(C_n exp(-i E_n t) sin(√(2 E_n) x)
  16.  
  17. Where C_n is arbitrary and the above represents a superposition of eigenstates,
  18. and E_n is the n-th energy level:
  19.  
  20. E_n = n²π²/2
  21.  
  22. """
  23.  
  24. import scipy as sp
  25. import scipy.linalg as la
  26. import matplotlib.pyplot as plt
  27. import matplotlib.animation as anim
  28.  
  29.  
  30. def energy_level(n):
  31. return (n**2 * sp.pi**2) / 2
  32.  
  33.  
  34. def wavefunction(x, t, coeff):
  35. n = sp.arange(len(coeff))
  36. E_n = energy_level(n)
  37. return (
  38. sp.sum(coeff * sp.exp(1j * t * E_n) * sp.sin(sp.sqrt(2*E_n) * x))/2
  39. )
  40.  
  41.  
  42. coefficients = sp.rand(5)
  43. coefficients = coefficients / la.norm(coefficients)
  44.  
  45. fig = plt.figure()
  46. ax = plt.axes(xlim=(0, 1), ylim=(0, 1))
  47. line, = ax.plot([], [])
  48.  
  49.  
  50. def init():
  51. line.set_data([], [])
  52. return line,
  53.  
  54.  
  55. def animate(t):
  56. xs = sp.linspace(0.0, 1.0, 200)
  57. ys = sp.array([wavefunction(x, 0.003 * t, coefficients) for x in xs])
  58. line.set_data(xs, ys * sp.conj(ys))
  59. return line,
  60.  
  61. anim = anim.FuncAnimation(fig, animate, init_func=init, frames=1000,
  62. interval=16, blit=False)
  63.  
  64. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement