我正在尝试绘制阶跃函数,当它们相加时将构成高木函数。第一个阶跃函数的图形正常,但第二个阶跃函数的图形不正常。请参见下面的 MWE(第一个为黑色,第二个为红色)。如何修复?
\documentclass[border=5mm]{standalone}
\usepackage{amsmath,amssymb}
\usepackage{pgfplots}
\tikzset{
declare function={Floor(\x)=round(\x-0.5);},
declare function={Ceil(\x)=round(\x+0.5);},
declare function={Distance(\x)=min(\x-Floor(\x),Ceil(\x)-x);},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend style={at={(0.5,-0.2)},anchor=north}
]
\addplot [
mark=none,
domain=0.0001:2,
samples=500,
smooth,
thick,black,
] {Distance(x)};
\addlegendentry{$x\mapsto \varphi(x)
:=\min\{x-\lfloor x\rfloor,\lceil x\rceil-x\}
:=\operatorname{dist}(x,\mathbb{Z})$}
\addplot [
mark=none,
domain=0.00001:2,
samples=500,
smooth,
thick,red,
] {Distance(2*x)/2};
\addlegendentry{$x\mapsto \frac{1}{2}\varphi(2x)$}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
\x
您只是忘记了函数定义中的最后一个反斜杠Distance
。正如评论中提到的,pgfmath
已经定义了floor
和ceil
,因此您不必自己定义它们。
\documentclass[border=5mm]{standalone}
\usepackage{amsmath,amssymb}
\usepackage{pgfplots}
\tikzset{
declare function={Distance(\x)=min(\x-floor(\x),ceil(\x)-\x);},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
legend style={at={(0.5,-0.2)},anchor=north}
]
\addplot [
mark=none,
domain=0.0001:2,
samples=500,
smooth,
thick,black,
] {Distance(x)};
\addlegendentry{$x\mapsto \varphi(x)
:=\min\{x-\lfloor x\rfloor,\lceil x\rceil-x\}
:=\operatorname{dist}(x,\mathbb{Z})$}
\addplot [
mark=none,
domain=0.00001:2,
samples=500,
smooth,
thick,red,
] {Distance(2*x)/2};
\addlegendentry{$x\mapsto \frac{1}{2}\varphi(2x)$}
\end{axis}
\end{tikzpicture}
\end{document}