1

I want to draw the Lemniscate of Bernoulli. Its polar equation is r^2=a^2\cos(2\theta), so it should be quite straightforward to plot. Choosing a=2, I tried with the following

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{polar}
\pgfplotsset{compat=1.10}

\begin{document} \begin{tikzpicture} \begin{polaraxis}[style=black!10,grid style=black!10,ticklabel style=black!50,enlargelimits=false, xticklabel=$\pgfmathprintnumber{\tick}^\circ$] \addplot [thick, red, domain=0:360, samples=100] {2sqrt(cos(2x))}; \end{polaraxis} \end{tikzpicture} \end{document}

but the result is

enter image description here

which is not nice at all. Moreover, I also get a lot of errors similar to

NOTE: coordinate (1Y2.3272287e2],3Y0.0e0]) has been dropped because it is unbounded (in y). (see also unbounded coords=jump).

I can improve the look of the plot by increasing the samples, but this increases compilation time and the number of errors without ever getting smooth enough.

I don't understand what is going on.

As a workaround, I can get a better looking figure using

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{polar}
\pgfplotsset{compat=1.10}

\begin{document} \begin{tikzpicture} \begin{polaraxis}[style=black!10,grid style=black!10,ticklabel style=black!50,enlargelimits=false, xticklabel=$\pgfmathprintnumber{\tick}^\circ$] \addplot [thick, green, domain=-45:45, samples=100] {2cos(2x)}; \addplot [thick, green, domain=135:225, samples=100] {2cos(2x)}; \end{polaraxis} \end{tikzpicture} \end{document}

which produces the figure

enter image description here

From a qualitative point of view this is the same, but from the mathematical point of view it is a completely different thing and this is not satisfying.

Note that this figure compiles without errors and is quite smooth even with a small number of samples.

brad
  • 517
  • In your fist code, just change the number of samples: samples=361 and you'll have it. – Juan Castaño Jan 11 '22 at 11:21
  • @JuanCastaño : You are right: it works. Yet, I do not understand why. And I still get lots of errors (178 of them, to be precise) – brad Jan 11 '22 at 11:25
  • samples=81,smooth looks also fine. – Stefan Pinnow Jan 11 '22 at 11:31
  • You get the errors because of mathematics. cos of 90° to 270° is negative and the square route of a negative number is not defined (except in a complex plane) ... – Stefan Pinnow Jan 11 '22 at 11:41
  • @StefanPinnow: what a fool I have been... :( Ok, I see that if I use two lines , one with domain=-45:45 and the second with domain=135:225, then it compiles without errors. Is there a way to have a unique line (i.e., a domain which is the union of the two domains)? – brad Jan 11 '22 at 11:55
  • I also note that this takes care of the number of samples: using \addplot [thick, red, domain=-45:45, samples=100] {2*sqrt(cos(2*x))}; then the figure is always smooth regardless of the samples number (provided they are enough...) – brad Jan 11 '22 at 11:57
  • Yes, you can use samples at={-45,-40,-35,...,45,135,140,...,225} instead of domain/samples. But to always get a smooth curve, you should add the option smooth as well. – Stefan Pinnow Jan 11 '22 at 12:09
  • And as an advice: You should use \addplot+ (instead of \addplot) for debugging, i.e. to see where the samples are. From that you get a good idea what might be "wrong"/can be improved. When the plot is finished, you can remove the markers (again), if you don't need them. That would have helped you to find out why your first image looks that "distorted". – Stefan Pinnow Jan 11 '22 at 12:13
  • Thank you very much: if you want to write a full answer, I will be glad to accept it. – brad Jan 11 '22 at 12:17

1 Answers1

3

As already stated in the comments below the question, you get the distorted result because you used improper sampling. You could have made this obvious by showing the markers (replace \addplot with \addplot+).

Then you would have seen that you need sampling points at all cos(θ) = 0 angles, i.e. 0°, 90°, 180°, 270°, to ensure that all features of the curve are visible. So the main task is to choose appropriate domain and samples values or directly state appropriate samples using samples at.

To ensure a smooth curve, add the smooth option.

% used PGFPlots v1.18.1
\documentclass{standalone}
\usepackage{pgfplots}
    \usepgfplotslibrary{polar}
    \pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}
    \begin{polaraxis}[
        style=black!10,
        grid style=black!10,
        ticklabel style=black!50,
        enlargelimits=false,
        xticklabel=$\pgfmathprintnumber{\tick}^\circ$,
        % ensure a smooth curve
        smooth,
    ]
        % choose appropriate `domain` and `sample` values
        \addplot+ [
            thick,
            domain=-45:225,
            samples=55,
        ] {2*sqrt(cos(2*x))};
    % alternatively "manually" state the samples to use
    \addplot+ [
        samples at={-45,-40,-35,...,45,135,140,...,225},
    ] {2*sqrt(cos(2*x))};
\end{polaraxis}

\end{tikzpicture} \end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535