# Make pgfplot lines dashed automatically below maximum of functions

I want to create a graph which displays linear functions whose maximum builds a piecewise-linear-function (PWLF) labeled max in the minimum working example below. Currently, to highlight the maximum, I'm adding a plot which calls max() and as arguments, takes the linear functions and overlays another plot in thick black to show the PWLF. The contributing functions are drawn as dashed manually to highlight the fact that they are "not contributing" in certain regions.

Really, what I'd like to do is have all the lines be dashed if they are not part of the maximum and otherwise be solid, to retain the color information in the PWLF as to which linear function makes up that segment of the plot.

Is there any way to do this descriptively, ideally without having to draw out the line segments manually? I really don't want to have to calculate the intersections for each example and then create appropriate plots every time.

The MWE currently generates this output:

MWE:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{positioning}
\usepackage{amsmath}

\pgfplotsset{
legend entry/.initial=,
every axis plot post/.code={%
\pgfkeysgetvalue{/pgfplots/legend entry}\tempValue
\ifx\tempValue\empty
\pgfkeysalso{/pgfplots/forget plot}%
\else
\fi
},
}

\begin{document}

\begin{figure}
\centering

\begin{tikzpicture}

\begin{axis}[width=\textwidth, enlargelimits=false, legend pos=outer north east, ytick=\empty, xtick={0, 1}, %
xticklabels={1 - $S_0$, $S_0$}]
\addplot[blue, dashed, thick, no marks, domain=0:1, legend entry=$a_1$]%
({x},{0.8 - 0.5*x});%

\addplot[olive, thick, dashed, no marks, domain=0:1, legend entry=$a_2$]%
({x}, {0.6 - 0.2*x});%

\addplot [red, thick, dashed, no marks, domain=0:1, legend entry=$a_3$]%
({x}, {0.3+0.4*x});%

\addplot [orange, dashed, thick, no marks, domain=0:1, legend entry=$a_4$]%
({x}, {0.5+0.1*x});%

\addplot [black, ultra thick, no marks, domain=0:1, legend entry=$\text{max}$] {max(0.8-0.5*x,0.3+0.4*x, 0.5+0.1*x)};

\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}


The sagetex package can handle this. It's documentation is located on CTAN here. This allows you to farm out the work to a computer algebra system, SAGE, which has its website here. This means you will need to download SAGE to your computer and install it properly or open up a free Cocalc account. If you have a Cocalc account, just paste create a LaTeX document, copy/paste the code below into your document, save it, and then press build to see the result. EDIT: I've modified the code to fix a mistake in the legend and to make it look more like the plot the OP has posted. Putting max function into the legend colors it solid blue so I've left it out.

\documentclass[border=4pt]{standalone}
\usepackage{sagetex}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\begin{document}
\begin{sagesilent}
f1 = 0.8-0.5*x
f2 = 0.6-0.2*x
f3 = 0.3+0.4*x
f4 = 0.5+0.1*x
t = var('t')
LowerY = 0.0
UpperY = 1.0
LowerX = 0.0
UpperX = 1.0
step = .001

f1x = [t for t in srange(LowerX,UpperX,step) if     f1(t)==max(f1(t),f2(t),f3(t),f4(t))]
f1y = [f1(t) for t in f1x]
f2x = [t for t in srange(LowerX,UpperX,step) if f2(t)==max(f1(t),f2(t),f3(t),f4(t))]
f2y = [f2(t) for t in f2x]
f3x = [t for t in srange(LowerX,UpperX,step) if f3(t)==max(f1(t),f2(t),f3(t),f4(t))]
f3y = [f3(t) for t in f3x]
f4x = [t for t in srange(LowerX,UpperX,step) if f4(t)==max(f1(t),f2(t),f3(t),f4(t))]
f4y = [f4(t) for t in f4x]

output = r""
output += r"\begin{tikzpicture}[scale=.7]"
output += r"\begin{axis}[width=\textwidth, enlargelimits=false, legend pos=outer north east, ytick=\empty, xtick={0, 1}, xticklabels={1-$S_0$,$S_0$}]"

output += r"\addplot[blue, dashed, thick, no marks, domain=0:1]"
output += r"({x},{0.8 - 0.5*x});"
output += r"\addlegendentry{$f1$}"
output += r"\addplot[olive, thick, dashed, no marks, domain=0:1]"
output += r"({x}, {0.6 - 0.2*x});"
output += r"\addlegendentry{$f2$}"
output += r"\addplot [red, thick, dashed, no marks, domain=0:1]"
output += r"({x}, {0.3+0.4*x});"
output += r"\addlegendentry{$f3$}"
output += r"\addplot [orange, dashed, thick, no marks, domain=0:1]"
output += r"({x}, {0.5+0.1*x});"
output += r"\addlegendentry{$a4$}"

if len(f1x)>1:
for i in range(0,len(f1x)-1):
output += r"(%f,%f) "%(f1x[i],f1y[i])
output += r"};"

if len(f2x)>1:
for i in range(0,len(f2x)-1):
output += r"(%f,%f) "%(f2x[i],f2y[i])
output += r"};"

if len(f3x)>1:
for i in range(0,len(f3x)-1):
output += r"(%f,%f) "%(f3x[i],f3y[i])
output += r"};"

if len(f4x)>1:
for i in range(0,len(f4x)-1):
output += r"(%f,%f) "%(f4x[i],f4y[i])
output += r"};"

output += r"\end{axis}"
output += r"\end{tikzpicture}"
\end{sagesilent}
\sagestr{output}
\end{document}


The output in Colcalc is shown below: A close up view:

Python is the language used in SAGE. After defining your 4 lines as f1 through f4, the code f1x = [t for t in srange(LowerX,UpperX,step) if f1(t)==max(f1(t),f2(t),f3(t),f4(t))] f1y = [f1(t) for t in f1x] creates a list of x and y coordinates for line f1. A point is added to f1x if f1 is the maximum point on all 4 lines, in which case the y value is added into f1y. It might be the case (such as 3 lines intersecting at the same point) that there is only 1 x-value for which a line achieves the maximum, in which case we don't plot that; any line truly part of the max function will have at least two points at which it achieves the max. So assuming f1 has more than one point in it, checked by if len(f1x)>1: the line will be plotted in as part of the max function.