# How should I handle NDsolve in Manipulate?

delta = -0.823
g = 0.000005

sol =
NDSolve[
{a'[t] == -I*a[t] (delta + g*Re[b[t]]) - a[t]*0.04 /2,
b'[t] == -I (b[t]*delta + g*Abs[a[t]]^2) - a[t]*0.09/2,
a == 1, b == 1},
{a, b}, {t, 0, 200}]
ParametricPlot[{Re[b'[t]], Re[b[t]]} /. sol, {t, 0, 200}]


I wish to use Manipulate to control the value of delta and g, but don't know how to handle the output from NDsolve when doing dynamic plotting. Can someone give me some guidance on Manipulate?

• Try Manipulate[sol=NDSolve[...];ParametricPlot[...],{delta,-1,0},{g,0,0.0005}] and see what you get. – Bill Nov 19 '20 at 4:54
• @Bill It returns "0 cannot used as a variable". I tried to change the values to {-0.923,-0,823} and it returns "0.923 cannot used as a variable" now. – BenXylona Nov 19 '20 at 5:10
• @Bill Thank you! I had already solved the problem by adding Clear before the codes. You are right, thank you for your advice! – BenXylona Nov 23 '20 at 5:37

Manipulate[
sol = NDSolve[{a'[t] == -I*a[t] (delta + g*Re[b[t]]) - a[t]*0.04/2,
b'[t] == -I (b[t]*delta + g*Abs[a[t]]^2) - a[t]*0.09/2, a == 1,
b == 1}, {a, b}, {t, 0, 200}];
ParametricPlot[{Re[b'[t]], Re[b[t]]} /. sol, {t, 0, 200},
PerformanceGoal -> "Quality"], {g, 0.000001,
0.00001}, {delta, -0.823 - 0.01, -0.823 + 0.01}]

• Maybe add TrackedSymbols :> Manipulate to keep it from an extraneous update after sol is changed (during an update in which a control is changed), though on a human scale, the MWE is fast enough as is. – Michael E2 Nov 20 '20 at 15:35
• @MichaelE2 Thank you! – cvgmt Nov 20 '20 at 15:44

You might consider the following variant of cvgmtj's answer. It has some performance advantages.

Manipulate[
NDSolveValue[
{a'[t] == -I*a[t] (delta + g*Re[b[t]]) - a[t]*0.04/2,
b'[t] == -I (b[t]*delta + g*Abs[a[t]]^2) - a[t]*0.09/2, a == 1,
b == 1},
{a, a', b, b'}, {t, 0, 200}];
ParametricPlot[{Re[bdF[t]], Re[bF[t]]}, {t, 0, 200},
PerformanceGoal -> "Quality"],
{aF, None},
{bF, None},
{bdF, None},
{g, 0., 0.0005, Appearance -> "Labeled"},
{delta, -0.823 - 0.25, -0.823 + 0.25, Appearance -> "Labeled"},
TrackedSymbols :> {g, delta}] I might not have posted this variant because the performance improvement is not all that noticeable, except that I want you to bring a further variant, which eliminates g, to your attention. I can see no visible difference in the plot produced from the following code form the plot produced by the preceding code. Can you?

Manipulate[
NDSolveValue[
{a'[t] == -I*a[t] delta - a[t]*0.04/2,
b'[t] == -I b[t] delta - a[t]*0.09/2,
a == 1, b == 1},
{a, a', b, b'}, {t, 0, 200}];
ParametricPlot[{Re[bdF[t]], Re[bF[t]]}, {t, 0, 200},
PerformanceGoal -> "Quality"],
{aF, None},
{bF, None},
{bdF, None},
{delta, -0.823 - 0.25, -0.823 + 0.25, Appearance -> "Labeled"},
TrackedSymbols :> {delta}] Makes me wonder if g is actually a significant variable. Perhaps your mathematical model can be profitably simplified were you to eliminate g.

• Thank you m_goldberg! The g in this equation is the most important term which represents coupling efficiency. The reason it has not much impact in the figure is the span range not big enough. A BIG thanks for your help! And also could u give me some suggestions on how to find more examples about manipulate? – BenXylona Nov 19 '20 at 17:49
• @BenXylona. Searching on the tag manipulate on this site should give you lots of examples. – m_goldberg Nov 19 '20 at 23:36
• @BenXylona. I added the tag *manipulate to your question. All you have do see more than a thousand examples is to click on that tag. – m_goldberg Nov 20 '20 at 0:06
ClearAll[reParametricListLinePlot];

reParametricListLinePlot[
ifs : {_InterpolatingFunction, _InterpolatingFunction},
opts : OptionsPattern@ListLinePlot] :=
ListLinePlot[Transpose[Re@#@"ValuesOnGrid" & /@ ifs], opts];

Manipulate[
reParametricListLinePlot[
NDSolveValue[
{a'[t] == -I*a[t] (delta + g*Re[b[t]]) - a[t]*0.04/2,
b'[t] == -I (b[t]*delta + g*Abs[a[t]]^2) - a[t]*0.09/2,
a == 1, b == 1},
{b', b}, {t, 0, 200}],
InterpolationOrder -> 3, AspectRatio -> 1],
{{delta, -0.823}, -2, -0.01, Appearance -> "Labeled"},
{{g, 0.000005}, 0.000001, 0.0001, Appearance -> "Labeled"}
] 