我想定义一个分段函数q(x)
,并尝试将解决方案调整为关于使用 pgfmathdeclarefunction 创建单位脉冲函数的这个问题,这样就没问题了。但是,当我尝试绘制时q(x+4)+0.5
,得到的图形并不是我所期望的。但是,对上述链接中的单位脉冲函数应用相同的变换就可以了。
那么,有没有更好的方法来定义分段定义函数?
下面的 MWE 产生以下结果。
p(x)
请注意,左侧的图表对于和都是符合预期的p(x+4)+0.5
。右侧的图表对于是正确的q(x)
,但对于是不正确的q(x+4)+0.5
。
\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\newcommand{\pLabel}{
$p(x)=
\begin{cases}
1 & 0 < x < 1\\
0 & \text{otherwise}
\end{cases}$
}
\newcommand{\qLabel}{
$q(x)=
\begin{cases}
x & 0 < x < 1\\
0 & \text{otherwise}
\end{cases}$
}
\newcommand{\pShiftedLabel}{$p(x+4)+0.5$}
\newcommand{\qShiftedLabel}{$q(x+4)+0.5$}
\pgfmathdeclarefunction{p}{1}{%
\pgfmathparse{(and(#1>0, #1<1))}%
}
\pgfmathdeclarefunction{q}{1}{%
\pgfmathparse{(and(#1>0, #1<1)*x)}%
}
\tikzstyle{MyStyle}=[domain=-5:5, samples=50, ultra thick]
\tikzstyle{pLabelStyle}=[above, yshift=22ex, xshift=-10ex]
\tikzstyle{qLabelStyle}=[below, yshift=-2ex, xshift=-10ex]
\tikzstyle{ShiftedLabelStyle}=[above left, xshift=1ex]
\begin{document}
%------------------ Using \pgfmathdeclarefunction -----------
Plot of $p(x)$ and \pShiftedLabel using PGF Version \pgfversion, followed by a plot of $q(x)$ and \qShiftedLabel
\begin{tikzpicture}
\begin{axis}
\addplot[MyStyle, blue]{p(x)} node [pLabelStyle] {\pLabel};
\addplot[MyStyle, red]{p(x+4)+0.5} node [ShiftedLabelStyle] {\pShiftedLabel};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}
\addplot[MyStyle, blue]{q(x)} node [qLabelStyle] {\qLabel};
\addplot[MyStyle, red]{q(x+4)+0.5} node [ShiftedLabelStyle] {\qShiftedLabel};
\end{axis}
\end{tikzpicture}
% --------------------- Using "declare function" -------------
Using declare function to define localp(x) and localq(x):
\begin{tikzpicture}
[declare function={localp(\t) = and(\t > 0, \t < 1);}]
\begin{axis}
\addplot[MyStyle, blue]{localp(x)} node [pLabelStyle] {\pLabel};
\addplot[MyStyle, red]{localp(x+4)+0.5} node [ShiftedLabelStyle] {\pShiftedLabel};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
[declare function={localq(\t) = (and(\t > 0, \t < 1)*x);}]
\begin{axis}
\addplot[MyStyle, blue]{localq(x)} node [qLabelStyle] {\qLabel};
\addplot[MyStyle, red]{localq(x+4)+0.5} node [ShiftedLabelStyle] {\qShiftedLabel};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
定义分段函数的两种方法都可以,但是你应该使用
\pgfmathdeclarefunction{q}{1}{%
\pgfmathparse{(and(#1>0, #1<1)*#1)}%
}
而不是and(#1>0, #1<1)*x)
,并且
[declare function={localq(\t) = (and(\t > 0, \t < 1)*\t);}]
而不是[declare function={localq(\t) = (and(\t > 0, \t < 1)*x);}]
,因为您实际上并不希望函数值为x
,而是参数的值(x+4
在本例中)。
\documentclass{article}
\usepackage{amsmath}
\usepackage{pgfplots}
\newcommand{\pLabel}{
$p(x)=
\begin{cases}
1 & 0 < x < 1\\
0 & \text{otherwise}
\end{cases}$
}
\newcommand{\qLabel}{
$q(x)=
\begin{cases}
x & 0 < x < 1\\
0 & \text{otherwise}
\end{cases}$
}
\newcommand{\pShiftedLabel}{$p(x+4)+0.5$}
\newcommand{\qShiftedLabel}{$q(x+4)+0.5$}
\pgfmathdeclarefunction{p}{1}{%
\pgfmathparse{(and(#1>0, #1<1))}%
}
\pgfmathdeclarefunction{q}{1}{%
\pgfmathparse{(and(#1>0, #1<1)*#1)}%
}
\tikzstyle{MyStyle}=[domain=-5:5, samples=100, ultra thick]
\tikzstyle{pLabelStyle}=[above, yshift=22ex, xshift=-10ex]
\tikzstyle{qLabelStyle}=[below, yshift=-2ex, xshift=-10ex]
\tikzstyle{ShiftedLabelStyle}=[above left, xshift=1ex]
\begin{document}
%------------------ Using \pgfmathdeclarefunction -----------
Plot of $p(x)$ and \pShiftedLabel using PGF Version \pgfversion, followed by a plot of $q(x)$ and \qShiftedLabel
\begin{tikzpicture}
\begin{axis}
\addplot[MyStyle, blue]{p(x)} node [pLabelStyle] {\pLabel};
\addplot[MyStyle, red]{p(x+4)+0.5} node [ShiftedLabelStyle] {\pShiftedLabel};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
\begin{axis}
\addplot[MyStyle, blue]{q(x)} node [qLabelStyle] {\qLabel};
\addplot[MyStyle, red]{q(x+4)+0.5} node [ShiftedLabelStyle] {\qShiftedLabel};
\end{axis}
\end{tikzpicture}
% --------------------- Using "declare function" -------------
Using declare function to define localp(x) and localq(x):
\begin{tikzpicture}
[declare function={localp(\t) = and(\t > 0, \t < 1);}]
\begin{axis}
\addplot[MyStyle, blue]{localp(x)} node [pLabelStyle] {\pLabel};
\addplot[MyStyle, red]{localp(x+4)+0.5} node [ShiftedLabelStyle] {\pShiftedLabel};
\end{axis}
\end{tikzpicture}
%
\begin{tikzpicture}
[declare function={localq(\t) = (and(\t > 0, \t < 1)*\t);}]
\begin{axis}
\addplot[MyStyle, blue]{localq(x)} node [qLabelStyle] {\qLabel};
\addplot[MyStyle, red]{localq(x+4)+0.5} node [ShiftedLabelStyle] {\qShiftedLabel};
\end{axis}
\end{tikzpicture}
\end{document}