3
$\begingroup$

I have a function $\psi(x,n)$ in mathematica which is quite complicated. However, plugging in $n=0$ gives a simple form. Mathematica knows this (see below), however it is still unable to even evaluate the function at a point. Doing so gives no errors but the output never returns, even though it is a calculation I can do in my head.

At this point I am stumped... here is the output which has me confounded:

enter image description here

Here is the function definition. $\psi$ is defined recursively as a function that remembers values it has found.

ψ0[x_] := 1/(2 π)^(1/4) Exp[-(x^2/4) - I k x];
ψr[x_?NumericQ, n_?NumericQ] := ψr[x, n] = 
   NIntegrate[
    SK[y, x, Δt] (
      Projector[x]/
      Sqrt[NIntegrate[Abs[Projector[x] ψr[x, n - 1]]^2, {x, -∞, ∞}]]
      ) ψr[x, n - 1], {y, -∞, ∞}];
ψr[x_, 0] := ψ0[x];

Here are the definitions which indirectly enter into the definition of the function:

ε = 0.2; 
Δt = 1.0;
L = 8.0;
tTypical = 10.0;
tMax = 20.0;
k = L/tTypical;
nMax = Floor[tMax/ Δt];
Projector[x_] := 1/(1 + Exp[-((L - x)/ε)]);
SK[x1_, x2_, t_] := 1/Sqrt[2 π I t] Exp[-((x1 - x2)^2/(2 I t))];

Edit: Here is a stack trace of the evaluation using TracePrint[ψr[0,0]]. Mathematica seems not smart enough to just plug in x, rather it is trying to do an integration. An elegant solution doesn't come to mind.

enter image description here


$\endgroup$
0

1 Answer 1

5
$\begingroup$

Order of definition matters, oddly enough. If you switch the order of definitions, things will work OK. I always define the specific cases first, then the general.

ψ0[x_] := 1/(2 π)^(1/4) Exp[-(x^2/4) - I k x];

ψr[x_, 0] := ψ0[x];

ψr[x_?NumericQ, n_?NumericQ] := ψr[x, n] = NIntegrate[SK[y, x, Δt] (Projector[x]/
   Sqrt[NIntegrate[Abs[Projector[x] ψr[x, n - 1]]^2, {x, -∞, ∞}]]) ψr[x,  n - 1], {y, -∞, ∞}];

ψr[10, 0]

$$ \frac{e^{-25-10 i k}}{\sqrt[4]{2 \pi }} $$

Set the constants and functions

ε = 0.2;
Δt = 1.0;
L = 8.0;
tTypical = 10.0;
tMax = 20.0;
k = L/tTypical;
nMax = Floor[tMax/Δt];
Projector[x_] := 1/(1 + Exp[-((L - x)/ε)]);
SK[x1_, x2_, t_] := 1/Sqrt[2 π I t] Exp[-((x1 - x2)^2/(2 I t))];

Try again

 ψr[10, 0]
(* -1.27631*10^-12 - 8.67854*10^-12 I *)

Note that this does not work

ψr[1, 2]
(* aborted *)

EDIT

For your function ψr[x, n] you have x as an input and also x as the iterator for the NIntegrate. Not sure what exactly you mean, but this runs...note the xx as an iterator, replacing x.

ψr[x_?NumericQ, n_?NumericQ] := ψr[x, n] = NIntegrate[SK[y, x, Δt] (Projector[x]/
   Sqrt[NIntegrate[Abs[Projector[xx] ψr[xx, n - 1]]^2, {xx, -∞, ∞}]]) ψr[x,  n - 1], {y, -∞, ∞}];

Running...

ψr[2, 2]
(* -0.00678479 - 0.23226 I *)
$\endgroup$
4
  • $\begingroup$ Hmm, that is a good point, switching the order makes it work for the n=0 case! But I still can't evaluate the function at other n, and the problem is (I think) the same problem that was causing the n=0 case not to work. What options do I have here? Is it really not possible to define psi in this way? $\endgroup$ Commented Feb 12, 2020 at 17:54
  • $\begingroup$ I think you have an issue with your function definition, see my updated answer. $\endgroup$
    – MikeY
    Commented Feb 12, 2020 at 18:13
  • $\begingroup$ Wow - that was really the issue. Thank you so much and sorry for the dumb mistake. $\endgroup$ Commented Feb 12, 2020 at 18:19
  • 1
    $\begingroup$ You just needed a fresh set of eyes on it. Best of luck. $\endgroup$
    – MikeY
    Commented Feb 12, 2020 at 18:24

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.