(* Mathematica program for Problem 3.7

  date : 26/9/2009
  author: Jorge C. Romão
  email : jorge.romao@tecnico.ulisboa.pt
*)

(* Definicoes das Funcoes da caixa *)

u=Function[{x,n,a},Sqrt[2/a] Sin[n Pi x/a] ]
up=Function[{x,m,a},Sqrt[2/a] Cos[(2 m -1) Pi x/a]]
um=Function[{x,m,a},Sqrt[2/a] Sin[2 m  Pi x/a]]

(* Calculo dos Coeficientes pares e impares *)

Apaux = Function[{n,a}, Integrate[Sqrt[2/a] up[x, n, a], {x, -a/2, 0}, 
      Assumptions -> n ∈ Integers]]

Ap=Function[n,(2*(-1)^n)/(Pi - 2*n*Pi)]

Amaux = Function[{n,a}, Integrate[Sqrt[2/a] um[x, n, a], {x, -a/2, 0}, 
      Assumptions -> n ∈ Integers]]

Am=Function[n, (-1 + (-1)^n)/(n*Pi)]

(* Energias dos estados pares e impares *)

Ep = Function[{n,a}, (2 n-1)^2/a^2]
Em = Function[{n,a}, (2 n)^2/a^2]

(* Funcao de onda para um instante t *)

psi=Function[{x,t,a,m},Sum[Ap[n] up[x,n,a] E^(-I t Ep[n,a]),{n,1,m}] + 
  Sum[Am[n] um[x,n,a] E^(-I t Em[n,a]),{n,1,m}] ]

movie=ListAnimate[Table[Plot[Abs[psi[x, t, 1, 100]]^2, {x, -1/2, 1/2}, 
   PlotRange -> {0, 2.5}], {t, 0, 2 Pi, Pi/4}]]