
我正在嘗試繪製階躍函數,求和後將構成高木函數。第一個階躍函數的圖形應該是這樣,但第二階躍函數的圖形失敗了。請參閱下面的 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}