3

I'd like to plot the following function between 0 and 0.5 using pgfplots.

function to plot

However I get as a result the fluctuating line in picture. I'd like to get a smooth line, as the function is.

Is this a numerical approximation error?

Here is a mwe

\documentclass[tikz]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}
    \begin{axis}[axis x line=middle, axis y line=middle,
             width=\linewidth, height=0.5\linewidth,
             xmin = 0, xmax=0.5]
    \addplot[smooth, domain=0:0.5, samples = 100] {
        exp(0.1*x)-sin(deg(0.1*x))
    }; 
    \end{axis}
\end{tikzpicture}
\end{document}

Output:

Result I get

3

Yes, these are numerical issues. In what follows I describe two ways to resolve them: using lualatex as compiler or using xfp for the computation. Both of them give smooth results, which agree.

Once I add \pgfplotsset{compat=1.16} and compile

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
\begin{tikzpicture}
    \begin{axis}[axis x line=middle, axis y line=middle,
             width=\linewidth, height=0.5\linewidth,
             xmin = 0, xmax=0.5]
    \addplot[smooth, domain=0:0.5, samples = 100] {
        exp(0.1*x)-sin(deg(0.1*x))
    }; 
    \end{axis}
\end{tikzpicture}
\end{document}

with lualatex I get

enter image description here

If you do not want to use lualatex you could employ xfp instead.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
%\pgfplotsset{compat=1.16}
\usepackage{xfp}
\begin{document}
\begin{tikzpicture}
\foreach \X [count=\Y] in {0,0.005,...,0.5}
{\ifnum\Y=1
\xdef\Lst{(\X,\fpeval{exp(0.1*\X)-sin(0.1*\X)})}
\else
\xdef\Lst{\Lst (\X,\fpeval{exp(0.1*\X)-sin(0.1*\X)})}
\fi}
    \begin{axis}[axis x line=middle, axis y line=middle,
             width=\linewidth, height=0.5\linewidth,
             xmin = 0, xmax=0.5,ytick=\empty]
    \addplot[smooth,no marks]  coordinates {\Lst}; 
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

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.