8

I want to draw $\sin(2\pi x) and $\cos 2 \pi x$ $\in [3/4, 5/4]$ in the same coordinate system. How do I do this ?

\begin{tikzpicture}

\begin{axis}[domain=3/4:5/4,legend pos=outer north east]
\addplot {sin(deg(x))}; 
\addplot {cos(deg(x))}; 

\legend{$\sin(2 \pi x)$,$\cos(2 \pi x)$}
\end{axis}
\end{tikzpicture}

By problem is the addplot. How do I plot it, {sin(deg(2\pi x))}; does not work?

10

In pgfplots, trigonometric functions take degrees as their default input. If we have a value measured in radians (like x in this case), we can convert it to degrees using the deg() function. Further multiplying x by 2*pi will produce a function with period of one radian.

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=3/4:5/4,legend pos=outer north east]
\addplot {sin(deg(2*pi*x))}; 
\addplot {cos(deg(2*pi*x))}; 
\legend{$\sin(2 \pi x)$,$\cos(2 \pi x)$}
\end{axis}
\end{tikzpicture}
\end{document}

Edit: As pointed out by percusse in the comments, a more global fix is to use trig format=rad or trig format plots=rad. The former changes the trigonometric format for all arguments, while the latter does so only for trigonometric functions (sin, cos, tan, etc.) An example follows:

\documentclass{standalone}
\usepackage{tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=3/4:5/4,legend pos=outer north east,trig format plots=rad]
\addplot {sin(2*pi*x)}; 
\addplot {cos(2*pi*x)}; 
\legend{$\sin(2 \pi x)$,$\cos(2 \pi x)$}
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

0
8

Erik's answer is perfect, but here is another answer with pgfplots. I wanted to show some features you can use to draw your plots such as defining functions, labelling axis, etc. Please note that instead of defining functions, you could easily plot functions by writing them in front of the \addplot code line.

enter image description here

% pdflatex
\documentclass{standalone}
    \usepackage{tikz}
    \usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
       \pgfmathdeclarefunction{S}{2}{\pgfmathparse{sin(deg(#1*#2))}}
       \pgfmathdeclarefunction{C}{2}{\pgfmathparse{cos(deg(#1*#2))}}
\begin{axis}
        [
    xtick={
        -6.28318, -4.7123889, -3.14159, -1.5708,
        1.5708, 3.14159, 4.7123889, 6.28318
    },
    xticklabels={
        $-2\pi$, $-\frac{3\pi}{2}$, $-\pi$, $\frac{\pi}{2}$,
        $\frac{\pi}{2}$, $\pi$, $\frac{3\pi}{2}$, $2\pi$
    },
    axis lines = center,
    grid=both,minor tick num=1,
    xlabel=$x$,ylabel=$y$,
    tick align=inside,
        legend style={at={(0.5,-0.1)},
        anchor=north,legend columns=2},
        legend entries={$\sin 2x$\\$\cos 2x$\\},
        domain=-2*pi:2*pi,
        samples=200,
        every axis y label/.style={rotate=0, black, at={(0.5,1.05)},},
        every axis x label/.style={rotate=0, black, at={(1.05,0.5)},},
        ]
        \addplot [red,thick] {S(2,x)};
        \addplot [blue,thick] {C(2,x)};
\end{axis}
\end{tikzpicture}
\end{document}

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.