# Drawing a sawtooth waveform ontop of a sine wave PGF plots

I have a fully rectified sinusoidal wave that I am trying to place a sawtooth wave form on top of as the image bellow,

I originally had a messy solution but it was not very good, I have a tex sine wave im trying to place it on bellow.

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain=0:1.5*360,
samples=4*360,
xtick=\empty,
width=10cm, height=4cm,
ymin=0,
enlarge x limits=false
]

\end{axis}
\end{tikzpicture}
\end{document}

• Creating such a plot looks easy enough given either a data file or formula for the sawtooth. Could you explain where you issue is, exactly? – Joseph Wright Oct 5 '14 at 6:01
• Yeh well thats my question, I can plot a sawtooth by drawing lines but what formula would I use..this is where im at.. \addplot [very thick] {0.5-atan(cot((pi*x)/2))}; – JS60 Oct 5 '14 at 7:13
• well throwing in some magic numbers i have a solution for it,\addplot [very thick] {0.91+0.001*atan(cot(95+ ((0.63*pi*x)/2)))}; – JS60 Oct 5 '14 at 7:29

You can use 1 - 1/(3*180)*mod(x+90,180) to plot a sawtooth wave. mod(x,180) repeats every 180 units, 1/(3*180) scales the values (1/180 would result in a sawtooth wave running from 0 to 1).

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain=0:1.5*360,
samples=4*360,
xtick=\empty,
width=10cm, height=4cm,
ymin=0,
enlarge x limits=false
]
\end{axis}
\end{tikzpicture}
\end{document}


This is one possible solution where foreach loop is used with a line approximation with sawtooth waveform.

if one wants the line to touch the curve without penetrating into the sine wave then remove the vertical line and change \arch definition to 60

Code

\documentclass[border=5mm]{standalone}

\usepackage{pgfplots}

\begin{document}
\def\arch{90}
\begin{tikzpicture}
\begin{axis}[
domain=0:1.5*360,
samples=360,
xtick={0,90,180,270,360,450,540},
width=10cm, height=5cm,
ymin=0, ymax=1.5, xmin=0,
enlarge x limits=false
]