我希望根据简单函数的集合绘制一个函数。例如,某些函数的最大值:
\begin{tikzpicture}[]
\begin{axis}
\addplot[smooth, color=black]
{
max( 2*x, 3*x, 4*x, 5*x, 0)
};
\end{axis}
\end{tikzpicture}
如何使用生成此功能\foreach
?
以下似乎不起作用
\begin{tikzpicture}[]
\begin{axis}
\addplot[smooth, color=black]
{
max(
\foreach \k in {2,3,4,5}
{\k*x, }
0)
};
\end{axis}
\end{tikzpicture}
因为它给出了以下错误:
! Undefined control sequence.
\foreach ...@empty \let \pgffor@assign@after@code
=\pgfutil@empty \let \pgff...
l.164 };
答案1
\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.9}
\begin{document}
\begin{tikzpicture}[
declare function={
f1(\t) = 5-5*\t ;
f2(\t) = 4-2*\t ;
f3(\t) = 3-\t ;
f4(\t) = 2-.5*\t ;
f5(\t) = e^(\t-4) ;
Max(\t) = max( {f1(\t)},
{f2(\t)},
{f3(\t)},
{f4(\t)},
{f5(\t)}
% {f6(\t)},
% {f7(\t)},
% {f8(\t)},
% {f9(\t)},
) ;
}
]
\begin{axis}[xmin=0,xmax=5,ymax=7]
\addplot[color=black,smooth,samples=400]
{
Max(x)
};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
答案egreg
在这里:\expandafter, \expandbefor :还有什么?:
\begin{document}
\Liste{\n-\n/(6-\n)*x}{5}
\bigskip
\begin{tikzpicture}
\begin{axis}[xmin=0,xmax=5,samples=400]
\Liste[\foo]{\n-\n/(6-\n)*x}{5}
\addplot[color=black,smooth,samples=100]
{
max(\foo,e^(x-4))
};
\end{axis}
\end{tikzpicture}
\end{document}