5
$\begingroup$

I wrote a simple 2D-phase portrait sample and still have a unsolvable question about adding a new feature on the plot. Specifically, I would like to drop down a point on the plot by each mouse-click and let it run down the vector stream as defined in the code. Here is the definition:

Vdot[V_, W_, Current_, a_] := V (a - V) (V - 1) - W + Current;
Wdot[V_, W_, b_, c_] := b V - c W;
pstream[{Xcenter_, Ycenter_, MagFactor_}, Current_, a_, b_, c_] := 
  StreamPlot[{Vdot[V, W, Current, a], Wdot[V, W, b, c]}, {V, 
    Xcenter - 2.5/(2*MagFactor), Xcenter + 2.5/(2*MagFactor)}, {W, 
    Ycenter - 1/(2*MagFactor), Ycenter + 1/(2*MagFactor)}, 
   PerformanceGoal -> "Quality", StreamStyle -> Gray];
pcontour[{Xcenter_, Ycenter_, MagFactor_}, Current_, a_, b_, c_] := 
  ContourPlot[{Vdot[V, W, Current, a] == 0, 
    Wdot[V, W, b, c] == 0}, {V, Xcenter - 2.5/(2*MagFactor), 
    Xcenter + 2.5/(2*MagFactor)}, {W, Ycenter - 1/(2*MagFactor), 
    Ycenter + 1/(2*MagFactor)}, 
   ContourStyle -> {Directive[Orange, Dashed], 
     Directive[Blue, Dashed]}];
pnorm[{Xcenter_, Ycenter_, MagFactor_}, Current_, a_, b_, c_] := 
  DensityPlot[Sqrt[
   Vdot[V, W, Current, a]^2 + Wdot[V, W, b, c]^2], {V, 
    Xcenter - 2.5/(2*MagFactor), Xcenter + 2.5/(2*MagFactor)}, {W, 
    Ycenter - 1/(2*MagFactor), Ycenter + 1/(2*MagFactor)}, 
   PlotTheme -> "Detailed", PlotLegends -> Automatic, 
   PlotStyle -> Opacity[0.75]];
Showbox2D[{Xcenter_, Ycenter_, MagFactor_}, Current_, a_, b_, c_] := 
  Show[pnorm[{Xcenter, Ycenter, MagFactor}, Current, a, b, c], 
   pstream[{Xcenter, Ycenter, MagFactor}, Current, a, b, c], 
   pcontour[{Xcenter, Ycenter, MagFactor}, Current, a, b, c], 
   GridLines -> Automatic];

Plot sample:

PlotSample

Anyone got an idea for breakthrough? Thank you in advance.

$\endgroup$
5
$\begingroup$

I would do this using a Manipulate with a Locator pane, because that's easier than using an EventHandler for mouse clicks.

First, define a function that calculates the additional stream line that you need, then superimpose that onto the already existing plots:

linePlot[V_, W_, Current_, a_, b_, c_, tMax_] := Module[{x, y},
  NDSolveValue[{D[{x[t], y[t]}, t] == {Vdot[x[t], y[t], Current, a], 
      Wdot[x[t], y[t], b, c]}, x[0] == V, y[0] == W}, {x[t], y[t]}, {t, 0, tMax}]]

interactiveBox2D[{Xcenter_, Ycenter_, MagFactor_}, Current_, a_, b_, c_, tMax_] := 
 DynamicModule[{static = 
    Showbox2D[{Xcenter, Ycenter, MagFactor}, Current, a, b, c]},
  Manipulate[
   Show[
    static,
    ParametricPlot[
     Evaluate@linePlot[x[[1]], x[[2]], Current, a, b, c, tMax], {t, 0, tMax}, 
       PlotStyle -> Red]], {x, {Xcenter - 2.5/(2*MagFactor), 
     Ycenter - 1/(2*MagFactor)}, {Xcenter + 2.5/(2*MagFactor), 
     Ycenter + 1/(2*MagFactor)}, Locator}]]

interactiveBox2D[{0, .4, 1}, 1/2, 1, 1, 1, 10]

manipulate

In the functions interactiveBox2D and linePlot, I added an additional parameter tMax that says how long the flow line will be in terms of its parameter t.

$\endgroup$
  • $\begingroup$ Thank you for the answer and sorry for being late. $\endgroup$ – Lance Sep 25 '16 at 9:39

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.