3
$\begingroup$

I wanted to show an animation for the time evolution of a function, and I chose the harmonic oscillator because I can write the evolution more easily. When I choose a specific t the first graph works great, so I think the temporal evolution works fine. The problem is that the animation is static when I used just Animate, and when I use ListAnimate I got the image that I will put below.
In the sum it should have been an exponential, that here it is written with E for some reason, at the power -i, that is written with I. I don't know why it's written like this when I paste it, but I wanted to specify it to not be any confusion.

xM = 20; 
Nx = 1024; 
dx = 2xM / Nx; 
m=1;
\[HBar]=1;
\[Omega] =1;
\[Alpha]=Sqrt[m \[Omega]/\[HBar]];
X = Table[-xM + i * dx, {i,1,Nx}];
\[u][n_,x_]=Sqrt[\[Alpha]/(2^n n! Sqrt[\[Pi]])]HermiteH[n, \[Alpha] x]E^(-\[Alpha]^2 x^2/2);

Ny = 5;
c = 1/(Ny + 1); 
\[CapitalPsi] = Sum[c*\!\(TraditionalForm\`u[j, X]\), {j, 0, Ny}];

Tm = 10;
dt = 1;
En[n_] = \[HBar] \[Omega] (1/2 + n);
\[CapitalPhi] = 
  Sum[E^(-I*En[j]*t)*c*\!\(TraditionalForm\`u[j, X]\), {j, 0, Ny}];
t = 10;

ListLinePlot[{Table[{X[[i]], Abs[\[CapitalPhi][[i]]]^2}, {i, 1, Nx}]},
  PlotRange -> All]

ListAnimate[
 ListLinePlot[{Table[{X[[i]], Abs[\[CapitalPhi][[i]]]^2}, {i, 1, 
     Nx}]}, PlotRange -> All], {t, 0, Tm, dt}, 
 AnimationRunning -> False]

The image that I talked about in the beginning

$\endgroup$
1
  • 1
    $\begingroup$ For a start, change [u][n_,x_] to u[n_,x_] and remove the backslash in front of it. $\endgroup$ May 8, 2023 at 20:34

2 Answers 2

4
$\begingroup$
Clear["Global`*"]
xM = 20;
Nx = 1024;
dx = 2 xM/Nx;
m = 1;
ℏ = 1;
ω = 1;
α = Sqrt[m ω/ℏ];
X = Table[-xM + i*dx, {i, 1, Nx}];
u[n_, x_] = 
  Sqrt[α/(2^n n! Sqrt[π])] HermiteH[n, α x] E^(-α^2 x^2/2);

Ny = 5;
c = 1/(Ny + 1);
Ψ = Sum[c*u[j, X], {j, 0, Ny}];

Tm = 10;
dt = 1;
En[n_] = ℏ ω (1/2 + n);

Φ[t_] = Sum[E^(-I*En[j]*t)*c*u[j, X], {j, 0, Ny}];

Using Table to build the data for the plot is highly inefficient

AbsoluteTiming[
  data1[t_] = Table[{X[[i]], Abs[Φ[t][[i]]]^2}, {i, 1, Nx}];][[1]]

(* 25.0747 *)

AbsoluteTiming[data2[t_] = Transpose[{X, Abs[Φ[t]]^2}];][[1]]

(* 0.028742 *)

data1[t] === data2[t]

(* True *)

ListLinePlot[data2 /@ {0, 5, 10}, PlotRange -> All,
 PlotLegends -> Placed[
   LineLegend[{0, 5, 10},
    LegendLabel -> Style[HoldForm@t, 14]], {.8, .5}]]

enter image description here

ListAnimate[
 Table[
  ListLinePlot[data2[t], PlotRange -> All],
  {t, 0, Tm, dt}],
 AnimationRunning -> False]

enter image description here

$\endgroup$
6
$\begingroup$

Your calculation is a bit lengthy, so it is better to prepare all the plots, then plot them. Here is one way to do this:

Define a function to calculate the wave function at time t

mkWaveFunction[t_] := (
   xM = 20; 
   Nx = 1024; 
   dx = 2 xM / Nx; 
   m = 1;
   ℏ = 1;
   ω = 1;
   α = Sqrt[m ω/ℏ];
   X = Table[-xM + i * dx, {i, 1, Nx}];
   u[n_, x_] = 
     Sqrt[α/(2^n n! Sqrt[π])] HermiteH[n, α x] E^(-α^2 x^2/2);
   
   Ny = 5;
   c = 1/(Ny + 1); 
   Ψ = Sum[c*u[j, X], {j, 0, Ny}];
   
   Tm = 10;
   dt = 1;
   En[n_] = ℏ ω (1/2 + n);
   Φ = Sum[E^(-I*En[j]*t)*c*u[j, X], {j, 0, Ny}];
   );

Generate a table of plots at t=0...10

myListOfPlots = Table[mkWaveFunction[t];
   Print["making plot at t = ", t];
   ListLinePlot[{Table[{X[[i]], Abs[Φ[[i]]]^2}, {i, 1,Nx}]}, 
     PlotRange -> All], {t, 0, 10, 1}];

This will print the progress as the plots are generated.

Animate the list of plots

ListAnimate[myListOfPlots]

This should produce the animation you want

enter image description here

To save as animated GIF

SetDirectory[NotebookDirectory[]]
Export["animation.gif", myListOfPlots, "DisplayDurations" -> 1]
$\endgroup$
2
  • $\begingroup$ Why do you put everything in the first part in the function "mkWaveFunction[t];". Like, the constants and the table for X? $\endgroup$ May 9, 2023 at 6:08
  • $\begingroup$ I put inside the function everything you need to calculate X and Φ to generate a plot. You can take things out of the function if you want (try it) or you can modify the function to take parameters like Nx as arguments. $\endgroup$
    – Themis
    May 9, 2023 at 10:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.