3

After I discovered the fpu library for increased precision, I tried to make a plot involving large exponents. I choosed as an example a plot of the gravitational force of the moon on a test mass in SI-units.

How can I make the following code to handle the large exponents and get a reasonable plot automatically.

\documentclass[tikz]{standalone}

\usepackage{pgfplots}
\newcommand{\td}[2]{\pgfmathsetmacro{#1}{#2}}
\pgfkeys{/pgf/fpu,/pgf/fpu/output format=sci}
\begin{document}

  \begin{tikzpicture}
    \begin{axis}
      %% Plot Gravitational force of the moon
      \td{\mM}{7.36e22} % mass of the moon in kg
      \td{\rM}{1.74e6} % radius of the moon in m
      \td{\mK}{1}% test mass in kg
      \td{\gG}{6.67e-11} % gravitational constant
      \addplot[domain=0:10e6] gnuplot {\gG*\mK*\mM/(\rM + x)^2};
    \end{axis}
  \end{tikzpicture}
\end{document}

Obviously one could choose more reasonable units for the example above, but my point is how pgfplots (with or without gnuplot) can handle plots with such high exponents directly.

  • Hmm. Strange and puzzled. gnuplot by itself is perfectly fine with the numbers. – Rmano Jun 24 '16 at 8:23
  • The error is ... puzzling too: ! Illegal unit of measure (pt inserted). <to be read again> e at the \begin{axis}. And yes, I have added compat settings. – Rmano Jun 24 '16 at 8:35
  • Probably related to tex.stackexchange.com/questions/36169/… --- @christian-feuersänger ? – Rmano Jun 24 '16 at 8:51
  • Who you use \pgfmathsetmacro to simply set values? Why don't you do \def\mM{7.36e22} instead of \td{\mM}{7.36e22}? Also, in conjunction with FPU, 7.36e22 is not a valid floating point format. – Henri Menke Jul 2 '16 at 12:52
  • Same problem when using \def. I didn't have any specific reason to use \pgfmathsetmacro I just thought that this is the standard way to define numbers for pgf. – student Jul 2 '16 at 12:57
1
+50

The pgfplots axes cannot handle FPU formatted values. You have to move the FPU instructions inside the axis environment and scope them.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\newcommand{\td}[2]{\pgfmathsetmacro{#1}{#2}}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
    %% Plot Gravitational force of the moon
    {
      \pgfkeys{/pgf/fpu,/pgf/fpu/output format=sci}
      \td{\mM}{7.36e22} % mass of the moon in kg
      \td{\rM}{1.74e6} % radius of the moon in m
      \td{\mK}{1}% test mass in kg
      \td{\gG}{6.67e-11} % gravitational constant
      \addplot[domain=0:10e6] gnuplot {\gG*\mK*\mM/(\rM + x)^2};
    }
  \end{axis}
\end{tikzpicture}
\end{document}

To simply set values to a macro, you don't need to invoke the math parser (this should also speed up typesetting a little). Gnuplot can handle large exponents, so there is not need to digest them through PGF first.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \begin{axis}
    %% Plot Gravitational force of the moon
    \def\mM{7.36e22} % mass of the moon in kg
    \def\rM{1.74e6} % radius of the moon in m
    \def\mK{1}% test mass in kg
    \def\gG{6.67e-11} % gravitational constant
    \addplot[domain=0:10e6] gnuplot {\gG*\mK*\mM/(\rM + x)^2};
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Great. Thanks a lot. However I don't really understand, what it means here, that the axis environment cannot handle FPU formatted values. Is this behaviour documented somewhere? – student Jul 2 '16 at 13:03
  • @student Internally an FPU float looks like 1Y2.17765411e23]. It is quite obvious why this cannot be parsed properly. I don't know if this is documented. – Henri Menke Jul 2 '16 at 13:06
  • I don't really understand it. Here is what I think: Suppose I define something like \pgfsetmacro{\V}{1.72e15} this is stored internally like 1Y1.72e15]. Then if I do something like \addplot gnuplot {x*\V*} the x is not stored in the FPU format, because the axis environment doesn't support it, but \V is and not proper formatted value can passed to gnuplot. But if this view is correct, I don't see why it helps to move the pgfkeys setting inside of the axis environment. – student Jul 2 '16 at 13:13
  • @student \addplot is perfectly aware of the FPU (this is documented), but things like the axis ticks and labels are not FPU-ready. That's why it helps to “hide” the FPU from the axis environment by grouping. – Henri Menke Jul 2 '16 at 13:19

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.