1
$\begingroup$
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[0] == 1, b[0] == 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?

$\endgroup$
3
  • 1
    $\begingroup$ Try Manipulate[sol=NDSolve[...];ParametricPlot[...],{delta,-1,0},{g,0,0.0005}] and see what you get. $\endgroup$ – Bill Nov 19 '20 at 4:54
  • 1
    $\begingroup$ @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. $\endgroup$ – BenXylona Nov 19 '20 at 5:10
  • $\begingroup$ @Bill Thank you! I had already solved the problem by adding Clear before the codes. You are right, thank you for your advice! $\endgroup$ – BenXylona Nov 23 '20 at 5:37
3
$\begingroup$
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[0] == 1,
     b[0] == 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}]
$\endgroup$
2
  • 1
    $\begingroup$ 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. $\endgroup$ – Michael E2 Nov 20 '20 at 15:35
  • $\begingroup$ @MichaelE2 Thank you! $\endgroup$ – cvgmt Nov 20 '20 at 15:44
1
$\begingroup$

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

Manipulate[
  {aF, adF, bF, bdF} =
    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[0] == 1,
       b[0] == 1}, 
     {a, a', b, b'}, {t, 0, 200}];
  ParametricPlot[{Re[bdF[t]], Re[bF[t]]}, {t, 0, 200}, 
    PerformanceGoal -> "Quality"],
  {aF, None},
  {adF, 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}]

manip1

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[
  {aF, adF, bF, bdF} =
    NDSolveValue[
      {a'[t] == -I*a[t] delta - a[t]*0.04/2, 
       b'[t] == -I b[t] delta - a[t]*0.09/2,
       a[0] == 1, b[0] == 1}, 
      {a, a', b, b'}, {t, 0, 200}];
  ParametricPlot[{Re[bdF[t]], Re[bF[t]]}, {t, 0, 200}, 
    PerformanceGoal -> "Quality"],
  {aF, None},
  {adF, None},
  {bF, None},
  {bdF, None},
  {delta, -0.823 - 0.25, -0.823 + 0.25, Appearance -> "Labeled"},
  TrackedSymbols :> {delta}]

manip2

Makes me wonder if g is actually a significant variable. Perhaps your mathematical model can be profitably simplified were you to eliminate g.

$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$ – BenXylona Nov 19 '20 at 17:49
  • $\begingroup$ @BenXylona. Searching on the tag manipulate on this site should give you lots of examples. $\endgroup$ – m_goldberg Nov 19 '20 at 23:36
  • $\begingroup$ @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. $\endgroup$ – m_goldberg Nov 20 '20 at 0:06
1
$\begingroup$
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[0] == 1, b[0] == 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"}
 ]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.