5

I'm new to latex and am trying to plot

    f(x)=sqrt{2/pi}*exp{-x^2/2}

I managed to plot the exponential part easily but when I multiply it by the square root 4 errors occur. This is my code:

    \documentclass{standalone}
    \usepackage{pgfplots}
    \begin{document}
    \begin{tikzpicture}
    \begin{axis} [ 
    xmin=0,xmax=4, 
    xmin=0,xmax=0.9, 
    xlabel=$x$,
    ylabel={$ f(x)=exp(-x^2/2)*sqrt(2/\pi) $}
    ] 
    \addplot { exp(-x^2/2)*sqrt(2/\pi) };
    \end{axis}
    \end{tikzpicture}
    \end{document}

all errors occur in line 11:

Missing $ inserted \addplot {exp(-x^2/2)*sqrt(2/\pi)};
Math formula deleted: Insufficient symbol fonts \addplot {exp(-x^2/2)*sqrt(2/\pi)};
Illegal unit of measure (pt inserted) \addplot {exp(-x^2/2)*sqrt(2/\pi)};
Extra \else \addplot {exp(-x^2/2)*sqrt(2/\pi)};

and a warning:

running in backwards compatibility mode (unsuitable tick labels; missing features). Consider writing \pgfplotsset{compat=1.14} into your preamble.

I tried defining two functions and \addplot ing their multiplication but it didn't work, can anyone help me

6
  • 4
    Welcome to TeX.SX! Use pi instead of \pi.
    – TeXnician
    Jul 4 '17 at 9:47
  • I also suggest you to change ylabel={$ f(x)=exp(-x^2/2)*sqrt(2/\pi) $} to ylabel={$ f(x)=e^{-x^2/2} \cdot (2/\pi)^2 $}. You might have overlooked the fact that the parametric instructions for PGFPLOTS will compile as a math equation in $$ environment. Also, you repetition of xmin and xmax is redundant; instead, type ymin=0,ymax=0.9 on the 7th line for the desired result. What you basically did is establish the limit of the plot and then reestablished it again (probably a human error). Have a nice day. I wish you a lot of success with LaTeX in the future!
    – God bless
    Jul 4 '17 at 10:15
  • @TeXnician Thanks. yes changing it from \pi to pi removed all the errors. Can't believe that was it!
    – Sara
    Jul 4 '17 at 10:28
  • @GregorPerčič Thanks for the suggestion I was wondering how to change it to look like that! Oops yes the repitition of x range was the result of copying and pasting the same line. Thanks that is really nice I wish you success in all areas too.
    – Sara
    Jul 4 '17 at 10:36
  • @Sara Thank you! I assume (don't be mad if I'm wrong) thta you are new to LaTeX. At first it IS hard, however, when you get used to it, you will never want to limit yourself with other text editors like for example Word. I'll post an answer covering all of the mistakes so others can learn from them (I hope that's good with you; comments are a little harder to spot than answers).
    – God bless
    Jul 4 '17 at 10:43
8

Your error is pretty simple: You used a command to typeset the pi symbol instead of a value which can be multiplied. This code works:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis} [ 
xmin=0,xmax=4, 
xmin=0,xmax=0.9, 
xlabel=$x$,
ylabel={$ f(x)=exp(-x^2/2)*sqrt(2/\pi) $}
] 
\addplot { exp(-x^2/2)*sqrt(2/pi) };
\end{axis}
\end{tikzpicture}
\end{document}
4

This isn't really an answer, but since I notice quite a few mistakes, it might help you. Of course, the credit for solving your problem definitely goes to TeXnician. Let's begin.

What we will fix:

  1. The redundant xmin, xmax
  2. The faulty compilation of ylabel
  3. The smoothness of your graph (OPTIONAL: I don't know if you want non-smooth graph for some personal reason)

For further explanation refer to my comment under your original question.

Here is the corrected code:

\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis} [ 
xmin=0,xmax=4, 
ymin=0,ymax=0.9, 
xlabel=$x$,
ylabel={$ f(x)=e^{-x^2/2} \cdot \sqrt{2/\pi} $},
axis lines=center,
axis equal
] 
\addplot[smooth, color=blue] { (exp(-x^2/2))*(sqrt(2/pi)) };
\end{axis}
\end{tikzpicture}
\end{document}

Explanations:

  1. Replaced xmin=0,xmax=0.9 with ymin=0,ymax=0.9
  2. Replaced ylabel={$ f(x)=exp(-x^2/2)*sqrt(2/\pi) $} with ylabel={$ f(x)=e^{-x^2/2} \cdot \sqrt{2/\pi} $}
  3. Added smooth to the \addplot tag (note that the same result can be achieved with samples=<some number>, and it gives you more freedom of optimization; for example, samples=200 would yield a similar result)

NOTE: I also changed the color of the plot (by adding color=blue to \addplot tag) to clarify the situation as much as possible. I also added axis lines=center and axis equal for convenience reasons. The first one changes the form of the axes (central rather than on boundaries) and the second one sets axes ratio to 1:1. I just added these two tweaks to make the whole structure clearer.

EDIT: For the sake of newcomers, I added the result of the compilation:

enter image description here

4
  • Great additions.
    – TeXnician
    Jul 4 '17 at 11:24
  • Thank you. Though I had some second thoughts: I was afraid that SE's (too) strict rules would mark my answer as "off-topic" because it didn't deal specifically with the original question's request. Glad to see that is not the case. Have a nice day.
    – God bless
    Jul 4 '17 at 11:43
  • Thanks these are great. I don't mean to be picky but since it's sqrt(2/pi) the ylabel should contain (2/\pi)^{0.5} not ^2
    – Sara
    Jul 4 '17 at 11:51
  • Yes, I noticed my mistake. I'll edit as soon as possible.
    – God bless
    Jul 4 '17 at 11:57

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.