2
$\begingroup$

I'd like to draw a graph, using recurrence table, but when I add normalization, the program stops working (it does not draw anything). Can you please help me fix it? For example:

Without normalization(works):

H[n_] := RecurrenceTable[{a[n + 1] == 2 x*a[n] - 2 n*a[n - 1], a[1] == 2 x, a[0] == 1}, a, {n, 1, 10}]
Plot [{H[n]*Exp[-x^2/2]/((2 (Pi)^1/2)^1/2)}, {x, -Pi, Pi}, {AxesLabel -> {p, \[Psi]}}]

With normalization( does not works):

H[n_] := RecurrenceTable[{a[n + 1] == 2 x*a[n] - 2 n*a[n - 1], a[1] == 2 x, a[0] == 1}, a, {n, 1, 10}]
d[n_] := H[n]/((2^n*n!)^1/2)
Plot [{d[n]*Exp[-x^2/2]/((Pi)^1/2)}, {x, -Pi, 
  Pi}, {AxesLabel -> {p, \[Psi]}}]
$\endgroup$

2 Answers 2

7
$\begingroup$

Your definition of H is slightly weird, because it has n as the argument, but then also uses n as an iterator inside RecurrenceTable. You don't actually need n as argument of H.

Then, after RecurrenceTable is done, there is no notion of n anymore, so the normalization function does not know what to put in as n, and it remains symbolic.

Let me show you fixed code:

(* Precompute 10 functions *)
H = RecurrenceTable[{a[n + 1] == 2 x*a[n] - 2 n*a[n - 1], 
     a[1] == 2 x, a[0] == 1}, a, {n, 1, 10}];

d[n_] := H[[n]]/((2^n*n!)^1/2)

Plot[Evaluate@Table[d[n]*Exp[-x^2/2]/((Pi)^1/2), {n, 1, 10}], 
     {x, -π, π}, AxesLabel -> {p, ψ}, PlotRange -> All]

enter image description here

$\endgroup$
2
  • $\begingroup$ Thanks a lot! I have another question, can I (after setting the function) plot a graph for a specific 'n'. For example: ClearAll[n] ClearAll[x] H = RecurrenceTable[{a[n + 1] == 2 xa[n] - 2 na[n - 1], a[1] == 2 x, a[0] == 1}, a, {n, 1, 10}]; d[n_] := H[[n]]/((2^n*n!)^1/2) n = 7 Plot[Evaluate@ Table[d[n]*Exp[-x^2/2]/((Pi)^1/2), {n, 1, 10}], {x, -[Pi], [Pi]}, AxesLabel -> {p, [Psi]}, PlotRange -> All] $\endgroup$ Commented Feb 28 at 12:58
  • $\begingroup$ @Sasha, sure, you can just do Plot[d[4]*Exp[-x^2/2]/((Pi)^1/2), {x, -π, π}]. $\endgroup$ Commented Feb 28 at 14:42
4
$\begingroup$
$Version

(* "14.2.0 for Mac OS X ARM (64-bit) (December 26, 2024)" *)

Clear["Global`*"]

Rather than use RecurrenceTable, use RSolve to get a closed form.

d[n_] = RSolveValue[{a[n + 1] == 2 x*a[n] - 2 n*a[n - 1], a[1] == 2 x, 
    a[0] == 1}, a[n], n]/((2^n*n!)^1/2)

(* (2^(1 - n) HermiteH[n, x])/n! *)

table = Tooltip[d[#]*Exp[-x^2/2]/((Pi)^1/2)] & /@ Range[10];

colors = ColorData[97] /@ Range[10];

Display one or more curves:

Manipulate[
 funcs = Sort[funcs];
 Plot[Evaluate@table[[funcs]], {x, -Pi, Pi},
  PlotStyle -> colors[[funcs]],
  AxesLabel -> {p, \[Psi]},
  PlotRange -> All],
 Row[{
   Control[{{funcs, {1}}, Range[10], ControlType -> TogglerBar}],
   Spacer[30],
   Button["All", funcs = Range[10]],
   Spacer[10],
   Button["Reset", funcs = {1}]}],
 TrackedSymbols :> {funcs}]

enter image description here

$\endgroup$
1

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.