# How to use StreamPlot to plot the typical solution in this directional field?

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

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.

## 2 Answers

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


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


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.

• 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! Jun 26, 2020 at 4:25
• @MichaelE2: How did you draw the last figure?
– Moo
Jun 26, 2020 at 12:06
• @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 Jun 26, 2020 at 17:53
• @MichaelE2: Thanks and I would love to see those updates! +1
– Moo
Jun 26, 2020 at 18:25