# How do use the results of NDSolve in the definition of another function?

I'm trying to plot a function of x and t in 1D, using manipulate to step through the time evolution of the function.

Manipulate[
Plot[Norm[χ[x, t]]^2, {x, -10, 10} ],
{t, 0, 1.82}]


I have defined the function

χ[x_, t_] :=
E^{
I[.5*(x - Evaluate[q[t] /. sol])^2 (Evaluate[a[t] /. sol] +
I Evaluate[b[t] /. sol]) + Evaluate[p[t] /. sol] (x -Evaluate[q[t] /. sol])]}


where a, b, q, p are time dependent functions that have been found using NDSolve,

sol =
NDSolve[
{q'[t] == -p[t]/m,
p'[t] == -E^(-1/(4 b[t])) Sin[q[t]],
b'[t] == 2 a[t]/m,
a'[t] == (a[t]^2 - b[t]^2)/m + E^(-1/(4 b[t])) Cos[q[t]],
q[0] == 0, p[0] == 1, a[0] == 1, b[0] == 1},
{q, p, a, b}, {t, 0 , 10}]


It's not clear to me from the documentation if this is the proper syntax for Evaluate or /.sol in this context. Manipulate does not return any kind of error, it just returns a blank plot.

• I've set m=1 in a previous line. NDSolve returns q,p,a,b and I can plot those individually as functions of time with no problem. May 2, 2018 at 2:16

Here, while defining chi, what you should be using is

\[Chi][x_,t_]:=E^(I(.5*(x-Evaluate[q[t]/.sol])^2 (Evaluate[a[t]/.sol]+I Evaluate[b[t]/.sol])+Evaluate[p[t]/.sol] (x-Evaluate[q[t]/.sol])))


You have used [ and { brackets where you should be using (. I [1] is different from I (1).

You could have figured it out by trying to find what \[Chi][1,1] gives you.

First, you need to rewrite your definition of χ[x_, t].

 χ[x_, t_] :=
E^I ((x - q[t])^2 (a[t]/2 + I b[t]) + p[t] (x - q[t]))


Note: the Wolfram Language only allows parentheses ( ) for grouping sub-expressions in an expression.

Second, I strongly suggest using NDSolveValue in place of NDSolve.

 Clear[q, p, b, a]
{q, p, b, a} =
With[{m = 100.},
NDSolveValue[
{q'[t] == -p[t]/m,
p'[t] == -E^(-1/(4 b[t])) Sin[q[t]],
b'[t] == 2 a[t]/m,
a'[t] == (a[t]^2 - b[t]^2)/m + E^(-1/(4 b[t])) Cos[q[t]],
q[0] == 0, p[0] == 1, a[0] == 1, b[0] == 1},
{q, p, a, b}, {t, 0, 10}]];


Note: I have selected an arbitrary value for m because you didn't give one.

Then

 Manipulate[
Plot[Norm[χ[x, t]]^2, {x, -10, 10}, PlotRange -> {Automatic, {0, 2*^4}}],
{t, 0, 10, ,5, Appearance -> "Labeled"}]


will give you a look at the behavior of χ over the domain {x, -10, 10} for various values of t.