我正在尝试使用脉冲来动画化叠加原理。如何避免脉冲边缘出现“耳朵”?
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{math, calc}
\usepackage{animate}
\begin{document}
\tikzstyle{curve} = [thick, domain = -3:3 , smooth, variable = \t, samples = 50, line cap = round]
\tikzmath{
function pulse(\t, \c, \a, \b){
\q = \t - \c;
if (\q < -\b/2.0)||(\q > \b/2.0) then {return 0.0;} else {return \a;};
};
function mycos(\t, \c, \a, \b){
\q = \t - \c;
\w = pi/\b;
if (\q < -\b/2.0)||(\q > \b/2.0) then {return 0.0;} else {return -\a*cos(\w*\q r);};
};
function doit(\t, \c, \a, \b){
return pulse(\t, \c, \a, \b);
%return mycos(\t, \c, \a, \b);
};
}
\begin{animateinline}[controls, loop, autoplay, buttonsize = 1em]{4}%
\multiframe{3}{rtime = -2.0 + 0.1}{%set 3 to 40 to have full animation
\begin{tikzpicture}[scale = 1.0]
\draw[thin, lightgray] (-3, -3) grid[step = 0.5] ++(6, 6);
\foreach \z in {-3, -2, ...,3} \node[below] at (\z, -3) {\tiny$\z$};
\foreach \z in {-3, -2, ...,3}\node[left] at (-3, \z) {\tiny$\z$};
\draw[curve, red, yshift = -2cm] plot({\t}, {doit(\t, -\rtime, -0.25, 1.0) + doit(\t, \rtime, 1.0, 1.0)});
\draw[curve, green, yshift = 2cm] plot({\t}, {doit(\t, \rtime, 1.0, 1.0)});
\draw[curve, blue] plot({\t}, {doit(\t, -\rtime, -0.25, 1.0)});
\draw (-3, -2) node[above right, black]{\tiny $x_1+x_2$};
\draw (-3, 0) node[above right, black]{\tiny $x_2$};
\draw (-3, 2) node[above right, black]{\tiny $x_1$};
\end{tikzpicture}
}%
\end{animateinline}
\end{document}
答案1
您可能正在寻找以下内容:
- “耳朵”是由
smooth
选项引起的。它使用样条函数在给定样本之间插入值。当样本之间或其中某些样本发生跳跃时,插值失败并产生可观察到的“耳朵”。 - 解决方法是删除此选项并增加样本数量
\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{math, calc}
\usepackage{animate}
\begin{document}
\tikzset{
curve/.style = {thick, domain = -3:3 , variable = \t, samples = 101, line cap = round}, % <---
N/.style = {font=\scriptsize, text=black, anchor=#1},
N/.default = south west
}
\tikzmath{
function pulse(\t, \c, \a, \b){
\q = \t - \c;
if (\q < -\b/2.0)||(\q > \b/2.0) then {return 0.0;} else {return \a;};
};
function mycos(\t, \c, \a, \b){
\q = \t - \c;
\w = pi/\b;
if (\q < -\b/2.0)||(\q > \b/2.0) then {return 0.0;} else {return -\a*cos(\w*\q r);};
};
function doit(\t, \c, \a, \b){
return pulse(\t, \c, \a, \b);
%return mycos(\t, \c, \a, \b);
};
}
\begin{animateinline}[controls, loop, autoplay, buttonsize = 1em]{4}%
\multiframe{3}{rtime = -2.0 + 0.1}{%set 3 to 40 to have full animation
\begin{tikzpicture}[scale = 1.0]
\draw[thin, lightgray] (-3, -3) grid[step = 0.5] ++(6, 6);
\foreach \z in {-3, -2, ...,3} \node[N=north] at (\z, -3) {$\z$};
\foreach \z in {-3, -2, ...,3} \node[N=east] at (-3, \z) {$\z$};
\draw[curve, red, yshift = -2cm] plot({\t}, {doit(\t, -\rtime, -0.25, 1.0) + doit(\t, \rtime, 1.0, 1.0)});
\draw[curve, green, yshift = 2cm] plot({\t}, {doit(\t, \rtime, 1.0, 1.0)});
\draw[curve, blue] plot({\t}, {doit(\t, -\rtime, -0.25, 1.0)});
\draw (-3, -2) node[N] {$x_1+x_2$};
\draw (-3, 0) node[N] {$x_2$};
\draw (-3, 2) node[N] {$x_1$};
\end{tikzpicture}
}%
\end{animateinline}
\end{document}
笔记:
tikzstyle
已经过时了。相反,它使用tikzset
或编写样式定义作为tikzpicture
- 在上面的 MWE(最小工作示例)中添加了样式节点及其默认锚点(此代码更短更清晰)