4

I want to plot the graph of a sequence relating to continuous compounding with some annual interest rate and an input capital. However, it seems that the precision of the generated plot is insufficient (see the graph fluctuations below).

Output:

enter image description here

MWE:

\documentclass[border=5pt]{standalone}%

\usepackage{tikz,pgfplots}


\begin{document}
\begin{tikzpicture}[font=\scriptsize]
\begin{axis}[%
    axis x line=bottom,
    axis y line=center,
    tick align=outside,
    axis y discontinuity=crunch,
    xtickmax=55,
    ytickmin=2100,
    ytickmax=2103,
    ymin=2098,
    enlargelimits=true,
    mark size=1pt,
    yticklabel style={/pgf/number format/1000 sep={\,}}
    ]
\addplot[%
    mark=none,
    domain=1:55,
    samples=55,
    only marks,
    orange!50,
    draw=black] {2000*(1+0.05/x)^x};
\end{axis}
\end{tikzpicture}
\end{document}

Question:

Is there any (simple) way for improving the precision of the \addplot-command (possibly without gnuplot)?

5

Here is an approach letting xint do the computations.

Fortunately only integer (half-integers would be ok) exponents are needed. Indeed, xint still waits for addition of support of mathematical functions such as log and exp.

Advantage is that it works will all engines (I used pdflatex).

\documentclass[border=5pt]{standalone}

\usepackage{tikz,pgfplots}

\usepackage{xintexpr}
\begin{document}
\begin{tikzpicture}[font=\scriptsize]
\begin{axis}[%
    axis x line=bottom,
    axis y line=center,
    tick align=outside,
    axis y discontinuity=crunch,
    xtickmax=55,
    ytickmin=2100,
    ytickmax=2103,
    ymin=2098,
    enlargelimits=true,
    mark size=1pt,
    yticklabel style={/pgf/number format/1000 sep={\,}}
    ]
\xintDigits:=8;% no need to use 16 digits of precision
\addplot[%
    mark=none,
%    domain=1:55,
%    samples=55,
    only marks,
    orange!50,
    draw=black] coordinates {%
      \xintthecoords% (converts x1,y1,x2,y2,... into (x1, y1) (x2, y2)...
                    %  format, as expected by "coordinates")
      \xintfloatexpr 
          seq((x,2000*(1+0.05/x)^x), x=1..55)
          % works with xint float engine
          % because exponent is integer (or half-integer)
      \relax
    };
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • Your approach works fine for me. I slightly changed the setting of precision digits to \xintDigits:=10. Now, the result is perfect for my needs. – Marian G. Aug 23 '18 at 9:53
  • 1
    luckily there were no sine or cosine... they need to be added to xint. If you entirely drop the \xintDigits line, computations will be done with 16 digits of (floating point) precision. It is also possible to set it to higher than 16 but of course this has no interest for plotting here and would only slow down pgfplots code parsing the coordinates. – user4686 Aug 23 '18 at 10:08
3

Warning: This answer is LuaLaTeX-based.

pgfplots warns you about one important detail: You should use a newer compatibility setting. Just copying the line from the warning to the file solves the fluctuations (as pgpfplots will use Lua to calculate the points).

compatibility mode

\documentclass{article}%

\usepackage{tikz,pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
\begin{tikzpicture}[font=\scriptsize]
\begin{axis}[%
    axis x line=bottom,
    axis y line=center,
    tick align=outside,
    axis y discontinuity=crunch,
    xtickmax=55,
    ytickmin=2100,
    ytickmax=2103,
    ymin=2098,
    enlargelimits=true,
    mark size=1pt,
    yticklabel style={/pgf/number format/1000 sep={\,}}
    ]
\addplot[%
    mark=none,
    domain=1:55,
    samples=55,
    only marks,
    orange!50,
    draw=black] {2000*(1+0.05/x)^x};
\end{axis}
\end{tikzpicture}
\end{document}
  • 1
    This is only true, if you run your code with LuaLaTeX, because with compat=1.12 or higher then Lua is used for the calculation. Of course Lua can't be used when compiling with pdfLaTeX. – Stefan Pinnow Aug 23 '18 at 7:58
  • 1
    @StefanPinnow Oops, there's a drawback having LuaLaTeX as default compiler :| I'll edit the post. – TeXnician Aug 23 '18 at 8:08
  • @TeXnician: Thank you for your reply. I forgott to mention the fact that I'm using pdfLaTeX (TeXLive 2017). Adding the line \pgfplotsset{compat=1.14} to my source does not change anything in the output in combination with pdfLaTeX. Unfortunatelly, I have no experiences with LuaLaTeX. – Marian G. Aug 23 '18 at 9:42
  • @MarianG. No problem, most people use pdflatex, I just forgot that the compatibility option is only relevant for lualatex for the reason Stefan Pinnow mentioned. Luckily jfbu's answer will work, so everything is fine :) – TeXnician Aug 23 '18 at 9:50

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.