Get envelope of a set of functions?
Michael E2 is correct in that Max
is remarkably capable with functions as well as values.
Defining
f1[x_] := -2 x + 2
f2[x_] := -x + 1.5
f3[x_] := x - 0.5
f4[x_] := 2 x - 2
funcs = {f1[x], f2[x], f3[x], f4[x]};
We can use Max
to get the function of the envelope:
m[x_] = PiecewiseExpand@Max[f1[x], f2[x], f3[x], f4[x]]
Plot[funcs, {x, 0, 2}, Epilog -> First@Plot[m[x], {x, 0, 2}, PlotStyle -> Directive[Dashed, Black]]]
Provided your functions are all linear we can safely solve for all the intersections and then examine which function is the largest in each region:
crossings = Union@N@Flatten[Outer[Solve[#1 == #2, x] &, funcs, funcs]][[;;, 2]];
Plot[funcs, {x, 0, 2}, Epilog -> {Line[{{#, -2}, {#, 2}}] & /@ crossings}]
Thus the different regions are bounded by:
regions = Join[{x < crossings[[1]]}, (#[[1]] < x < #[[2]]) & /@ Partition[crossings, 2, 1], {crossings[[-1]] < x}]
{x < 0.5, 0.5 < x < 0.833333, 0.833333 < x < 1., 1. < x < 1.16667, 1.16667 < x < 1.5, 1.5 < x}
Within which the orderings are:
Reverse /@ (Ordering@Through[{f1, f2, f3, f4}[#]] & /@ MovingAverage[
Join[{crossings[[1]] - 1}, crossings, {crossings[[-1]] + 1}], 2])
{{1, 2, 3, 4}, {2, 1, 3, 4}, {2, 3, 1, 4}, {3, 2, 4, 1}, {3, 4, 2, 1}, {4, 3, 2, 1}}
Let
f[x_] := {-x+2,-2x+3,1.1x-1,2x-3}
A=0;
B=3;
Plot[Evaluate@f[x],{x,A,B}]
With this,
Flatten[Ordering[#,-1]&/@f/@ Mean/@Partition[Sort@Flatten[{A,B,x/.Table[NSolve[{f[x][[i]]==f[x][[j]],A<=x<=B},x],{i,1,Length[f[0]]-1},{j,i+1,Length[f[0]]}]}],2,1]]//.{a___,b_,b_,c___}:>{a,b,c}
Outputs {2,1,3,4}
. This code works also for non-linear functions.
For example, if
f[x_] := {Sin[x],-2x^2+3,1.1x^3-4,2x-3,4Cos[7x]}
the output is {5, 2, 5, 1, 5, 3}
.