6

I have tikzpicture with sine waves. What I would want is that the red bouncy wave goes along the black sine wave until it hits the red dotted line. then drops to the blue dotted line and so on. I have now used straight lines because I don't know how to draw the red line on top of the black sine wave and then stop at the red dotted line. My tikzpicture currently

\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}
  • It would be nice if you added the picture so the community would understand what you are asking about before compiling the code to see it. – dustin Oct 5 '14 at 15:24
  • @dustin Don't you need a certain amount of reputation to do that? – cfr Oct 6 '14 at 1:12
  • 1
    @cfr dart was able to do it with one reputation though. – dustin Oct 6 '14 at 2:48
  • @dustin Thanks. Either I'm wrong or they've changed things and so I'm wrong. Hence, I'm wrong ;). Good to know. – cfr Oct 6 '14 at 11:02
5

I think the problem here is that to draw the function it is necessary to save a value between calls of the function (I can't see any other way of doing it, maybe I'm missing something). The following does this, although the constant \K would need be set to zero if the function was to be plotted twice.

\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.