4

I want to plot a function in a LaTeX document. Usually I use either Mathematica, Matlab or Root. At this point I want to use pgfplots in order to keep my document in a complete LaTeX layout. The thing is that pgfplots doesn't produce the exact plot as Mathematica, let alone the weird thing that's going on with the y-axis.

My code is

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[ 
    xlabel=$x$,
    ylabel={$d\sigma$}
  ] 
    \addplot[blue,domain=0:pi]{1-sin(x/2)^2}; 
  \end{axis}
\end{tikzpicture}
\end{document}

My output is

enter image description here

while Mathematica gives

enter image description here

Any ideas on that?

2
  • 2
    Use \addplot[blue,domain=0:3]{1-sin(deg(x/2))^2};. It needs degrees.
    – percusse
    Jun 29, 2013 at 8:07
  • @percusse: Thank you very much for your answer! You are absolutely right! Thank's alot! Would you mind answering the question, so as to accept you answer?
    – Thanos
    Jun 29, 2013 at 8:14

1 Answer 1

6

There are two problems with your code. First, the domain 1:2*pi means the interval [1, 2pi]. In Mathematica, you asked for the interval [0,pi]. So replacing 1:2*pi by 0:pi should give you the correct interval to draw.

The second problem is that the sin function expects an argument in degrees, not radians. To convert from radians to degrees, you can use the deg function. Replacing sin(x/2) by sin(deg(x)/2) will do the trick.

Manually overriding the minimum/maximum x and y values, and removing the frame can be done by setting the appropriate options. The following code produces more or less the same plot as your Mathematica plot:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
  \begin{axis}[ 
    xmin=0, xmax=3.4,
    ymin=0, ymax=1.1, 
    axis lines=left,
    xlabel=$x$,
    ylabel={$d\sigma$}
  ] 
    \addplot[blue,domain=0:pi]{1-sin(deg(x)/2)^2}; 
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

4
  • Thank yuo very much for your answer! About the first part, you are right! I found it out and while you were answering I was editing y code! About the second part, Thank you!!!!
    – Thanos
    Jun 29, 2013 at 8:15
  • Is there a way to plot the x-axis in degrees and a legend?
    – Thanos
    Jun 29, 2013 at 8:19
  • 1
    Yeah sure. Change the domain to 0:180 and remove the deg again.
    – yori
    Jun 29, 2013 at 8:19
  • To convert from radians to degrees, you may use x/2 r instead of deg(x)/2. Jun 29, 2013 at 8:23

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.