I have the following symmetric curves
Plot[{Exp[-x] Sin[5 x], -Exp[-x] Sin[5 x]}, {x, 0, 2 π}, PlotRange -> All]
I want to join all the maxima and minima, foming envelope above and below the curves. How can this be done?
Edit: plotting an NDSolve
result:
{g[x_], xm} = Reap[
NDSolveValue[ {
f''[x] == -2 (5 Cos[5 x] + 12 Sin[5 x] ) E^-x,
f[0] == 0, f'[0] == 5,
WhenEvent[f'[x] == 0 && f''[x] < 0, Sow[{x, f[x]}, 1]],
WhenEvent[f'[x] == 0 && f''[x] > 0, Sow[{x, f[x]}, 2]]},
f[x], {x, 0, 6}]]
Plot[g[x], {x, 0, 5}, Epilog -> {Line /@ xm}, PlotRange -> All]
//original version
exp = Exp[-x] Sin[5 x];
top = {#, Abs[exp /. x -> #]} & /@
Sort@Flatten[x /. NSolve[D[exp, x] == 0 && 0 < x < 6 , x]];
Show[{
Plot[Evaluate[exp {1, -1}], {x, 0, 2 \[Pi]}, PlotRange -> All],
ListPlot[{top, {1, -1} # & /@ top}, Joined -> True, PlotRange -> All]
}]
Update: For the case where
f1
andf2
are ... numerical solutions to a set of differential equations:
s = NDSolve[{x'[t] == -y[t] - x[t]^2, y'[t] == 2 x[t] - y[t]^3, x[0] == y[0] == 1},
{x, y}, {t, 21}];
ClearAll[f1, f2]
f1[z_] := First[x /. s][z];
f2[z_] := First[y /. s][z];
colors = ColorData[97, "ColorList"][[;; 2]];
plt = Show[Plot[{f1[x], f2[x]}, {x, 0, 20.1}, Filling-> 1 -> {2}, PlotRange -> All],
With[{f = #, col = First[colors = RotateRight[colors]]},
Plot[#[x], {x, 0, 20.1}, PlotStyle -> col, PlotRange -> All,
PlotRangeClipping -> False, MeshFunctions -> {f'[#] &}, Mesh -> {{0}}]]&/@ {f1, f2}];
Show[plt, ListLinePlot[SortBy[First] /@
GatherBy[Cases[Normal @ plt, Point[x_] :> x, ∞], NonPositive[Last@#]&],
Filling -> Axis, FillingStyle -> Opacity[.3, Yellow], PlotRange -> All,
Mesh -> Full], PlotRange->{{0,20},All}, ImageSize -> 600]
Original answer:
f1[x_]:=Exp[-x] Sin[5 x];
f2[x_]:=-Exp[-x] Sin[5 x];
plt = Plot[{f1[x],f2[x]}, {x, 0, 2 π}, PlotRange -> All, Filling -> 1 -> {2},
MeshFunctions -> {f1'[#]&,f2'[#]&}, Mesh->{{0},{0}}];
Show[plt, ListLinePlot[GatherBy[SortBy[Cases[Normal @ plt, Point[x_] :> x, ∞], Last],
Positive[Last @ #]&],
Filling -> Axis, FillingStyle -> Opacity[.5, Yellow],
PlotRange -> Full], PlotRange -> All, ImageSize->600]
NDSolve
you may want to use WhenEvent
to find the extrema for you.
$\endgroup$
Commented
Feb 1, 2018 at 21:41