1

I have a parabola, two ellipses, and a multiple of a horizontal shift of the cube root function drawn in the following TikZ diagram. Only the latter is drawn poorly. Using only TikZ, is there a way to draw it more accurately?

\documentclass[10pt]{amsart}

\usepackage{mathtools,array}

\usepackage{tikz} \usetikzlibrary{calc}

\begin{document}

\begin{center} \begin{tikzpicture}

%Part of two ellipses and a parabola are drawn. \draw (0,0) arc (0:90: 2 and 1); \draw (0,0) arc (0:-72: 2 and 1); \path node[anchor=east] at ({-2+sqrt(31)/8},-15/16){$E$}; \draw[fill] (0,0) circle (1.5pt); % \draw (3,0) arc (180:90: 2 and 1); \draw (3,0) arc (180:252: 2 and 1); \path node[anchor=west] at ({5-sqrt(31)/8},-15/16){$F$}; \draw[fill] (3,0) circle (1.5pt); % \draw[domain=-1:4, smooth, variable=\x, blue] plot ({\x}, {-1/9\x\x + 1/3*\x}); \path node[anchor=south west, xshift=-0.5mm, yshift=-1mm] at (3.75,-5/16){$G$}; % %A "pin" is drawn between (0,0) and its label. \draw[draw=gray, line width=0.8pt, shorten <=1.5mm, shorten >=1mm] (0,0) -- ({(atan(1/3)+90)/2}:0.75); \path node[anchor=south west, inner sep=0] (P) at ({(atan(1/3)+90)/2}:0.75 ){$P$}; % %A "pin" is drawn between (3,0) and its label Q. \draw[draw=gray, line width=0.8pt, shorten <=1.5mm, shorten >=2mm] (3,0) -- ($(3,0) +({(atan(-1/3)+180+90)/2}:0.75)$); \node[anchor=base, baseline={P.base}, inner sep=0] (Q) at ($(3,0) +({(atan(-1/3)+180+90)/2}:0.75)$) {$Q$};

% \draw[domain=1.5:4, smooth, variable=\x] plot ({\x}, {3/2pow((\x - 3/2),1/3) + 1/4}); \draw[domain=-1:1.5, smooth, variable=\x] plot ({\x}, {-3/2pow((3/2 - \x),1/3) + 1/4}); \draw[fill] (3/2,1/4) circle (1.5pt); \node[anchor=north west] at (3/2,1/4){$R$}; \path node[anchor=west] at (4,2.2858){$H$};

\end{tikzpicture} \end{center}

\end{document}

user143462
  • 1,039
  • 1
    You couly try using the option samples with a reasonably high value, such as \draw[domain=1.5:4, smooth, variable=\x, samples=200] plot ({\x}, {3/2*pow((\x - 3/2),1/3) + 1/4});. The higher the number, the more accurate the plot will essentially be, but compilation will also take longer. – Jasper Habicht Oct 27 '22 at 20:04
  • @Jasper Habicht I knew that samples is an option in pgfplots, but I did not know it was an option in just TikZ. Thanks. What is the default number of sample points? – user143462 Oct 27 '22 at 22:25
  • 1
    According to the manuals, the default value for samples is 25. This holds for /tikz/samples as well as for /pgfplots/samples. – Jasper Habicht Oct 27 '22 at 22:37
  • 1
    There might be a gap due to rounding errors at the end of the lower plot. To fix this, you can turn around the values for the domain: domain=1.5:-1. You can also simply add -- (1.5,0.25) to the path, which will add the last missing piece to the plot. Or you could try using samples at (see: https://tex.stackexchange.com/q/663247/47927). – Jasper Habicht Oct 27 '22 at 22:54
  • Remember that pgfmath is a dancing bear. The amazing thing is not how well it dances, but that it dances at all. – John Kormylo Oct 28 '22 at 01:54

2 Answers2

2

To make the curve smoother, you can use the samples option that takes as value an integer that represents the number of samples for the plot. Its default value is 25 (which holds true for /tikz/samples as well as for /pgfplots/samples). The larger the number, the smoother the plot will be, but also the longer compilation will take.

Due to rounding errors, there might be a gap at the end of the plot (near the coordinate (3/2,1/4)), which you can solve easily in this case by flipping the values for the domain option. This way, TikZ will start with drawing the plot at the coordinate (3/2,1/4) and, thus, in case there is a gap due to rounding errors, it will be at the other end of the plots and not that big a deal.

Another solution to close this gap would be to just add the last missing piece to the path by adding -- (3/2,1/4) to it, but doing so might be considered a quick-and-dirty fix. Also, using samples at as suggested in this answer could be a solution, but I somehow failed to to apply it here in a satisfactory way. Finally, as suggested in the same answer, using a sample value of 2 + 1 should also prevent such gaps, so you can go with samples=257, for example.

The following example shows how to use samples and uses flipped values for the domain option:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}

%Part of two ellipses and a parabola are drawn. \draw (0,0) arc (0:90: 2 and 1); \draw (0,0) arc (0:-72: 2 and 1); \path node[anchor=east] at ({-2+sqrt(31)/8},-15/16){$E$}; \draw[fill] (0,0) circle (1.5pt); % \draw (3,0) arc (180:90: 2 and 1); \draw (3,0) arc (180:252: 2 and 1); \path node[anchor=west] at ({5-sqrt(31)/8},-15/16){$F$}; \draw[fill] (3,0) circle (1.5pt); % \draw[domain=-1:4, smooth, variable=\x, blue] plot ({\x}, {-1/9\x\x + 1/3\x}); \path node[anchor=south west, xshift=-0.5mm, yshift=-1mm] at (3.75,-5/16){$G$}; % %A "pin" is drawn between (0,0) and its label. \draw[draw=gray, line width=0.8pt, shorten <=1.5mm, shorten >=1mm] (0,0) -- ({(atan(1/3)+90)/2}:0.75); \path node[anchor=south west, inner sep=0] (P) at ({(atan(1/3)+90)/2}:0.75 ){$P$}; % %A "pin" is drawn between (3,0) and its label Q. \draw[draw=gray, line width=0.8pt, shorten <=1.5mm, shorten >=2mm] (3,0) -- ($(3,0) +({(atan(-1/3)+180+90)/2}:0.75)$); \node[anchor=base, baseline={P.base}, inner sep=0] (Q) at ($(3,0) +({(atan(-1/3)+180+90)/2}:0.75)$) {$Q$}; % \draw[domain=1.5:4, smooth, variable=\x, samples=200] plot ({\x}, {3/2pow((\x - 3/2),1/3) + 1/4}); \draw[domain=1.5:-1, smooth, variable=\x, samples=200] plot ({\x}, {-3/2*pow((3/2 - \x),1/3) + 1/4}); \draw[fill] (3/2,1/4) circle (1.5pt); \node[anchor=north west] at (3/2,1/4){$R$}; \node[anchor=west] at (4,2.2858){$H$};

\end{tikzpicture}

\end{document}

enter image description here

0

If you have trouble sketching (especially where a graph goes vertical), a good approach is to try to find a parametric form of your graph. Sometimes that's difficult, but in this case you can invert your expression and plot x as a function of y. This matches the other answer's graph:

\documentclass[border=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}

\begin{tikzpicture}

%Part of two ellipses and a parabola are drawn. \draw (0,0) arc (0:90: 2 and 1); \draw (0,0) arc (0:-72: 2 and 1); \path node[anchor=east] at ({-2+sqrt(31)/8},-15/16){$E$}; \draw[fill] (0,0) circle (1.5pt); % \draw (3,0) arc (180:90: 2 and 1); \draw (3,0) arc (180:252: 2 and 1); \path node[anchor=west] at ({5-sqrt(31)/8},-15/16){$F$}; \draw[fill] (3,0) circle (1.5pt); % \draw[domain=-1:4, smooth, variable=\x, blue] plot ({\x}, {-1/9\x\x + 1/3\x}); \path node[anchor=south west, xshift=-0.5mm, yshift=-1mm] at (3.75,-5/16){$G$}; % %A "pin" is drawn between (0,0) and its label. \draw[draw=gray, line width=0.8pt, shorten <=1.5mm, shorten >=1mm] (0,0) -- ({(atan(1/3)+90)/2}:0.75); \path node[anchor=south west, inner sep=0] (P) at ({(atan(1/3)+90)/2}:0.75 ){$P$}; % %A "pin" is drawn between (3,0) and its label Q. \draw[draw=gray, line width=0.8pt, shorten <=1.5mm, shorten >=2mm] (3,0) -- ($(3,0) +({(atan(-1/3)+180+90)/2}:0.75)$); \node[anchor=base, baseline={P.base}, inner sep=0] (Q) at ($(3,0) +({(atan(-1/3)+180+90)/2}:0.75)$) {$Q$}; % \draw[domain=-1.78:2.28,variable=\y]plot({((\y-.25)2/3)^3+1.5},{\y}); %\draw[domain=1.5:4, smooth, variable=\x] plot ({\x}, {3/2pow((\x - 3/2),1/3) + 1/4}); %\draw[domain=-1:1.5, smooth, variable=\x] plot ({\x}, {-3/2pow((3/2 - \x),1/3) + 1/4}); \draw[fill] (3/2,1/4) circle (1.5pt); \node[anchor=north west] at (3/2,1/4){$R$}; \node[anchor=west] at (4,2.2858){$H$};

\end{tikzpicture}

\end{document}

Teepeemm
  • 6,708