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
\expandafter\addlegendentry\expandafter{\tempValue}%
\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}