1
$\begingroup$

How can I plot the typical solution in this directional field using StreamPlot?

The equation is:

f[x_, y_] := y - y^3
p1 = StreamPlot[{1, f[x, y]}, {x, -3, 3}, {y, -3, 3}, Frame -> False, 
  Axes -> True, AspectRatio -> 1/GoldenRatio, 
  AxesLabel -> {"x", "y(x)"}, BaseStyle -> 12, 
  StreamPoints -> {{{{-1, 0}, Red}, Automatic}}]

I want to plot the typical solution, for example, when -1<y<0 and y<-1.

$\endgroup$
0

2 Answers 2

3
$\begingroup$

Do you want something like this?

f[x_, y_] := y - y^3
p1 = StreamPlot[
  {1, f[x, y]},
  {x, -3, 3},
  {y, -3, 3},
  Frame -> False,
  Axes -> True,
  AspectRatio -> 1/GoldenRatio,
  AxesLabel -> {"x", "y(x)"},
  BaseStyle -> 12,
  StreamPoints -> {{
     {{-1, 0}, Red},
     {{-1, -3}, Green},      (* Only this line and the next are changed. *)
     {{-1, -1/2}, Orange},
     Automatic
    }}
 ]

StreamPlot with highligthed flow lines

$\endgroup$
2
$\begingroup$

Here is something taken from code I wrote many years ago. It works only for autonomous first-order ODEs, but it gives an alternative approach to the OP's example.

ClearAll[dfPortrait];
dfPortrait[vf_, {fn_, x1_, x2_}, {time_, t1_, t2_}, opts___?OptionQ] :=
    Module[{stationaryPts, intervals, eqns, x0}
   , plotrange = {All, {x1, x2}}
   ; padding = {0.1, (x2 - x1) {0.03, 0.02}}
   ; stationaryPts = 
    fn /. Solve[vf == 0 && x1 <= fn <= x2, fn, Reals] /. fn -> {}
   ; intervals = Partition[Union[N@{x1, x2}, N@stationaryPts], 2, 1]
   ; eqns = {Derivative[1][fn][time] == (vf /. fn -> fn[time]), 
     fn[0] == x0}
   ; With[{psol = ParametricNDSolveValue[eqns
       , fn, {time, t1, t2}, {x0}
       , "ExtrapolationHandler" -> {Indeterminate &, 
         "WarningMessage" -> False}
       ]}
    , Plot[
     Evaluate[Quiet[(psol[#1][time] &) /@
        Join[stationaryPts
         , If[(vf /. fn -> Mean[#1]) > 0
            , {0.9, 0.1}.#1
            , {0.1, 0.9}.#1
            ] & /@ intervals
         ]]]
     , {time, t1, t2}
     , PlotRange -> plotrange, PlotRangePadding -> padding , 
     ImageSize -> {Automatic, 150}, Ticks -> {None, stationaryPts}, 
     ExclusionsStyle -> None, AxesLabel -> {time, fn /. x_[0] :> x}
     ]]
   ];

dfPortrait[y - y^3, {y, -2, 2}, {x, 0, 3}]

enter image description here

I used it to illustrate autonomous first-order ODEs with figures like the following, where the v denotes the derivative ("velocity"), and the middle graph is the phase field on the line; this type diagram can be seen in V.I. Arnold's Ordinary Differential Equations.

enter image description here

$\endgroup$
4
  • $\begingroup$ It is really very illustrative. I appreciate it a lot. But my goal was to make it look a bit like this: 1.link2.link, to help to select the correct answer. Thank u, Michael! $\endgroup$ Jun 26, 2020 at 4:25
  • $\begingroup$ @MichaelE2: How did you draw the last figure? $\endgroup$
    – Moo
    Jun 26, 2020 at 12:06
  • 2
    $\begingroup$ @Moo It's an old function I wrote many years ago. The code needs refactoring, which I started yesterday but my stupid compute crashed unexpectedly (not due to Mma either) and lost the work. The function is called portrait1D and is contained in the old package, which I uploaded here: pastebin.com/raw/ra8sftKY $\endgroup$
    – Michael E2
    Jun 26, 2020 at 17:53
  • $\begingroup$ @MichaelE2: Thanks and I would love to see those updates! +1 $\endgroup$
    – Moo
    Jun 26, 2020 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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