3

I need to plot a function using pgfplots, but the result is different from the expected. The domain should be 0:1.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
    \begin{axis}[enlargelimits=false]
        \addplot[domain=0:55, samples=1001]{((sqrt((1 -cos(2*pi*x))^2+sin(2*pi*x)^2))^4)^2};
    \end{axis}
\end{tikzpicture}

\end{document}

Notice that the domain had to be set to 55 in order to show the whole function. While the MATLAB code being the following:

x=0:0.01:1;
plot(x,((sqrt((1-cos(2*pi*x)).^2+sin(2*pi*x).^2)).^4).^2)

I have read this answer but it did not help this case.

7

Matlab's sin and cos assume radians as input, while pgf's sin and cos assume degrees. For pgfplots you can add trig format=rad to the \addplot options to get the same behaviour as Matlab, then domain=0:1 works.

enter image description here

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{document}

\begin{tikzpicture}
    \begin{axis}[enlargelimits=false]
        \addplot[domain=0:1, samples=1001, trig format=rad]{((sqrt((1 -cos(2*pi*x))^2+sin(2*pi*x)^2))^4)^2};
    \end{axis}
\end{tikzpicture}
\end{document}
| improve this answer | |

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.