# Need help plotting a wavefunction generated from a sum of eigenstates [closed]

I am a beginner in Mathematica. I am trying to plot the time evolution of a non-eigenstate wavefunction as a sum of eigenstates.

So I start with an eigenstate of an infinite square well, then expand the well rapidly to twice its initial size, then I want to plot how the wavefunction evolves.

I got this error:

I am going to try to use Manipulate to evolve it after my Plot works

– Syed
Oct 1 '21 at 7:39
• At the first glance, I would say you have to clear the symbol x with Clear[x]. Oct 1 '21 at 7:55
• @Syed, I tried to paste my code, but things got messy. Cn[n_] := \!( *SubsuperscriptBox[([Integral]), (0), (L1)]( *FractionBox[(2), SqrtBox[(L1\ L2)]] Sin[\ *FractionBox[([Pi]), (L1)] x] Sin[n\ *FractionBox[([Pi]), (L2)] x] [DifferentialD]x)); Something like that. How do I paste the code correctly? Oct 1 '21 at 8:45
• I tried the 'Clear[x]' but it didn't work. This time Mathematica keeps running and I had to abort the evaluation Oct 1 '21 at 8:51
• To past your code cleanly, first convert it to InputForm. Also, have you tried saving your notebook, closing and reopening Mathematica, and then loading and running your notebook. Oct 1 '21 at 13:29

From the error message it looks like the variable $$x$$ has a value, 0.000204. What's happening is ψ2[0] is getting evaluated for each value of the plot variable, $$x$$. The value of $$x$$ is then being substituted into the expression for $$c_n$$ before the integration is carried out. That may be just as well, because the infinite sum is not going to finish evaluating anyway.

Let's make three changes. First, let's use $$\xi$$ as the variable of integration for the $$c_n$$'s. Second, let's use memoization to avoid recalculating each $$c_n$$ for every value of the plot variable. Third, let's use only a finite number of terms in the summation.

ClearAll["Global*"]
L1 = 1; L2 = 2; h = 1; m = 1;
nmax = 12;

ϕ[L_, n_, x_] := Sqrt[2/L] Sin[n π /L x]
En[L_, n_] := n^2 π^2 h^2/(2 m L^2)
Cn[n_] := Cn[n] = Integrate[2/Sqrt[L1 L2] Sin[π/L1 ξ] Sin[n π/L2 ξ], {ξ, 0, L1}]


Note the memoization in the previous line. That ensures each $$c_n$$ is only calculated once. Now define the finite summation and evaluate it at $$t=0$$

ψ2[t_] := Sum[Cn[n] ϕ[L2, n, x] Exp[-I En[L2, n]/h t], {n, 1, nmax}]
f = ψ2[0];


We can plot $$f$$ with Plot[f, {x, 0, 10}], which gives

The squiggly baseline in the plot is due to using only 12 terms in the summation. It may be interesting to look at how much time it takes to evaluate Plot[f,{x,0,10}] compared to Plot[ψ2[0],{x,0,10}].

The following timing results were obtained using nmax = 12

Plot[f, {x, 0, 10}] // Timing // First       (*  0.042195  *)
Plot[ψ2[0], {x, 0, 10}] // Timing // First   (*  0.414601  *)


The timing results lend credence to the earlier statement that the summation for ψ2[0] is being re-evaluated for each value of the plot variable.

### Using Manipulate

Here is a Manipulate that uses a common programming style and gives good (quick) results even for larger values of $$n_{max}$$

g = Norm[ψ2[t]]^2;
Manipulate[
Plot[g /. t -> λ, {x, 0, 10}, PlotRange -> {Full, {0, 2.5}}],
{λ, 0, 10}]


There are knowledgeable explanations in this forum of why the t -> λ replacement is necessary, but I don't remember any of them. As a rule, I try do as much of the calculation as possible outside of the Manipulate and then use the "slider replacement" technique inside the Manipulate.

• Thanks! I have managed to make it evolve in time using 'Manipulate'. But I have a few questions, why can't I use f in 'Manipulate'? I tried this 'Manipulate[Plot[Norm[f]^2, {x, 0, 2}], {t, 0, 10}]' but the graph stays still when I move the slider. I had to use 'Manipulate[Plot[Norm[\psi2 [t]]^2, {x, 0, 2}], {t, 0, 10}]' instead. Oct 1 '21 at 11:50
• Manipulate can be tricky to use. I can't give a good explanation of why some code does not work, but I have seen good explanations in this forum. It is possible to define $f$ outside the Manipulate` and then use the "slider replacement" technique, as I call it. Please see the new "Using Manipulate" section of the answer. Oct 1 '21 at 18:36
• Thanks again :) . Can you tell me how to paste code correctly? Oct 2 '21 at 10:51
• @ForacleFunacle My copy/paste procedure: In MMA, (1) select cells, (2) Shift-Ctrl-I to convert to input form, (3) Ctrl-C to copy. In SE editor pane, (a) Ctrl-V to paste, (b) select the inserted code block text, (c) click menu icon { } to indent the code block, (d) click menu icon $\alpha\beta$ to convert greek letters, etc, (e) edit the code for readability. Back in MMA (4) Ctrl-Z to undo the effect of Shift-Ctrl-I. See MMA.SE Meta site for more. Don't miss this answer Oct 3 '21 at 3:17