6

I have in \pgfmathsetmacros some stored variables. I checked that math on Mathematica when the plotted points came back wrong. \pm\infp is approx \pm 1.51022 and \pm\infn is approx \pm 0.46823. Then I need to determine their corresponding y values. The equation is x^2exp(-x^2). It appears that everything is set up correctly, but the plot only shows one point at around (-3, 0) which isn't in the ball park. Since everything appears correct, I am not sure what is going on or wrong.

\documentclass[tikz, convert = false]{standalone}%

\usepackage{pgfplots}
\pgfplotsset{compat = 1.10}

\begin{document}
\begin{tikzpicture}
  \pgfmathsetmacro{\infp}{sqrt(5 + sqrt(17))/2}
  \pgfmathsetmacro{\infn}{sqrt(5 - sqrt(17))/2}
  \pgfmathsetmacro{\yp}{(\infp)^2*exp(-(\infp)^2)}
  \pgfmathsetmacro{\yn}{(\infn)^2*exp(-(\infn)^2)}
  \begin{axis}[
    axis lines = middle,
    xmin = -3,
    xmax = 3,
    ymin = 0,
    ymax = .5,
    xlabel = {x},
    ylabel = {y},
    ytick = {0.4},
    unit vector ratio = 1 1 1
    ]
    \addplot[blue, smooth] gnuplot[domain = -3:3, samples = 500]
    {x^2*exp(-x^2)};

    \foreach \x/\y in {\infp/\yp, -\infp/\yp, \infn/\yn, -\infn/\yn}{
      \begingroup\edef\temp{%
        \endgroup\noexpand\filldraw[red] (\x, \y) circle[radius = 0.025];}
      \temp
    }
  \end{axis}
\end{tikzpicture}
\end{document}
4
  • Is -x^2=(-x)^2 or -x^2=-(x^2) ? Obviously power takes precedence over minus in normal math, but I do not think it is well defined in inline notation. Apr 23, 2014 at 15:52
  • @Hans-PeterE.Kristiansen The equation is x^2*exp(-x^2) so it is set up correctly for the math.
    – dustin
    Apr 23, 2014 at 15:53
  • When I try to run this I get an error for the line containing gnuplot. Is there something special I must do other than running pdflatex on the document file?
    – A.Ellett
    Apr 23, 2014 at 16:35
  • @A.Ellett if you want to run the file, you will need to do shell escape. Also, you may need to do a few runs since plot tables are generated. If you use latexmk, then one calling to that will work.
    – dustin
    Apr 23, 2014 at 16:59

1 Answer 1

7

pgfplots currently supports only the axis cs in order to write something into the axis.

Consequently, you need to write

\filldraw (axis cs:\x,\y) ... ;

and all is good.

1
  • Actually... well, I fear this is more my mistake/lack of courage. In the early days of pgfplots, I feared that reconfiguring the standard syntax (\x,\y) to use axis cs implicitly would cause lots of unanticipated bugs. Your question was the "kick-off" to convince me that this needs to done and can be done and I made an item on my todo list. It is about time to revisit some of the older and complicated limitations of pgfplots. Apr 24, 2014 at 5:13

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.