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