TransWikia.com

How should I handle NDsolve in Manipulate?

Mathematica Asked by BenXylona on April 28, 2021

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?

3 Answers

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}]

Answered by cvgmt on April 28, 2021

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.

Answered by m_goldberg on April 28, 2021

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"}
 ]

Answered by Michael E2 on April 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP