我有带正弦波的 tikzpicture。我想要的是红色的弹性波沿着黑色正弦波行进,直到碰到红色虚线。然后下降到蓝色虚线,依此类推。我现在使用直线,因为我不知道如何在黑色正弦波上方绘制红线,然后停在红色虚线上。
\documentclass[a4paper,english,12pt]{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[xscale=0.8]
%nodes
\node [below right] at (20,0) {$t$};
\node [above left] at (0,10) {$U$};
\node [above, green] at (3,8.4) {$U(t)$};
\node [above] at (3,5.66) {$u_c (t)$};
\node [above,red] at (8,4.5) {$u_{ci} (t)$};
\node [above,blue] at (8,0.5) {$u_{ce} (t)$};
%axis
\draw [<->,ultra thick] (0,10) -- (0,-10);
\draw[->,ultra thick] (0,0)--(20,0);
%dashed lines
\draw[red,dashed,thick] (0,4.5) --(18,4.5);
\draw[red,dashed,thick] (0,-4.5) --(18,-4.5);
\draw[blue,dashed,thick] (0,0.5) --(18,0.5);
\draw[blue,dashed,thick] (0,-0.5) --(18,-0.5);
%helpgrid
\draw[gray] (0,-10) grid (18,10);
%Green sine wave
\draw[green,thick] (0,0) sin (3,8.4) cos (6,0) sin(9,-8.4) cos(12,0) sin(15, 8.4) cos(18,0);
%black sine wave with lower amplitude
\draw[thick] (0,0) sin (3,5.66) cos (6,0) sin(9,-5.66) cos(12,0) sin(15, 5.66) cos(18,0);
%red bouncing waveform
\draw[red,ultra thick] (0,0) -- (1.7,4.5);
\draw[red, ultra thick] (1.7,4.5) -- (1.7,0.5);
\draw[red,ultra thick] (1.7,0.5) sin (3.2,2) cos (6.2,-4.5);
\draw[red,ultra thick] (6.2,-4.5) -- (6.2,-0.5);
\draw[red,ultra thick] (6.2,-0.5) -- (7.7,-4.5);
\draw[red,ultra thick] (7.7,-4.5) -- (7.7,-0.5);
\draw[red, ultra thick] (7.7,-0.5) sin (9, -2) cos (12.1, 4.5);
\draw[red,ultra thick] (12.1,4.5) -- (12.1,0.5) -- (13.7,4.5) -- (13.7, 0.5);
\draw[red,ultra thick] (13.7,0.5) sin (15,2) cos (16.3,0);
\end{tikzpicture}
\end{document}
答案1
我认为这里的问题是,要绘制函数,必须在函数调用之间保存一个值(我看不出还有其他方法可以做到这一点,也许我遗漏了什么)。下面的代码可以做到这一点,但\K
如果要绘制两次函数,则需要将常数设置为零。
\documentclass[tikz,border=5]{standalone}
\gdef\K{0}
\pgfmathdeclarefunction{msin}{3}{%
\pgfmathparse{sin(#1)+\K}%
\let\a=\pgfmathresult%
\ifdim\a pt>#2 pt\relax
\pgfmathparse{\K-(#2-#3)}%
\global\let\K=\pgfmathresult%
\fi%
\ifdim\a pt<-#2 pt\relax%
\pgfmathparse{\K+(#2-#3)}%
\global\let\K=\pgfmathresult%
\fi%
\let\pgfmathresult=\a%
}
\def\i{0.75}
\def\j{0.125}
\begin{document}
\begin{tikzpicture}[x=.125pt, line join=round, >=stealth]
\draw (-30,0) -- (570,0);
\draw (0,-1.5) -- (0,1.5);
\draw [red, dotted] (0, \i) -- (540, \i);
\draw [red, dotted] (0,-\i) -- (540,-\i);
\draw [blue, dotted] (0, \j) -- (540, \j);
\draw [blue, dotted] (0,-\j) -- (540,-\j);
\draw [green] (0,0) plot [domain=0:540, samples=100]
(\x, 1.125*sin \x);
\draw (0,0) plot [domain=0:540, samples=100]
(\x, sin \x);
\draw [red] (0,0) plot [domain=0:540, samples=250]
(\x, {msin(\x, \i, \j)});
\end{tikzpicture}
\end{document}