7

I am using following pgfplots to draw a simple function:

\begin{center}
\begin{tikzpicture}
\begin{axis}[
    domain=-4:4,
    xscale=2.25,yscale=1.125,
    xmin=-4, xmax=4,
    ymin=-2, ymax=2,
    samples=500,
    axis lines=center,
    grid=both,
    xtick={-4,-3,-2,-1,0,1,2,3,4}
]
    \addplot+[orange,mark=none,very thick] {rad(asin((2*x)/((x^2)+1)))};
\end{axis}
\end{tikzpicture}
\end{center}

It renders this plot:

Plot with wrong smoothness

As you see, the spikes are curved, although I have not used the "smooth" option in \addplot.

Does anyone know, if there is a way to make the spikes sharper?

(Sorry for my English.)

  • If you write \pgfplotsset{compat=1.12} (or newer) into your preamble and compile the example with lualatex, it will also be fine. This makes use of the lua backend and has higher accuracy – Christian Feuersänger Jan 15 '16 at 18:50
5

One solution is determine enough large number of samples so, that will one lie at each spike. Then the obtained image is:

enter image description here

Code:

\documentclass[border=3mm]{standalone}
    \usepackage{pgfplots}

    \begin{document}
\begin{tikzpicture}
\begin{axis}[
    domain=-4:4,
    xscale=2.25,yscale=1.125,
    xmin=-4, xmax=4,
    ymin=-2, ymax=2,
    samples=150,
    axis lines=center,
    grid=both,
    xtick={-4,-3,-2,-1,0,1,2,3,4}
]
    \addplot[orange,very thick,mark=none] {rad(asin((2*x)/((x^2)+1)))};
\end{axis}
\end{tikzpicture}
    \end{document}
  • 1
    Interestingly, in waran's code, more samples are used (500 instead of 150). I think this might have something to do with the approximations PGF uses for trigonometric functions. In this case, more samples actually leads to worse results. – Jake Jan 14 '16 at 20:51
  • I observe similarly. Number of samples over 150 gives me worse result. And number should be even. – Zarko Jan 14 '16 at 20:58
  • I wonder if samples at could be chosen to give good results... – cmhughes Jan 14 '16 at 21:01
  • @cmhughes, yes, this seems to be one option, however, manually set adequate number of samples fast become challenging. – Zarko Jan 14 '16 at 21:05
  • 1
    samples at={-4,-3.9,...,-1,-0.99,-0.9,...,1,1.01,1.1,...,4} works pretty well and is only slightly tedious. – Jake Jan 14 '16 at 21:46

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.