我正在尝试绘制如下曲线图:
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\draw[->] (-3,0) -- (3,0) node[right] {$x$};
\draw[->] (0,-3) -- (0,3) node[above] {$y$};
\draw[thick, domain=-2.2:-1, smooth, variable=\y, blue] plot [samples=500] ({pow(\y*\y -1, (1/3))},{\y});
\draw[thick, domain=-1:1, smooth, variable=\y, blue] plot [samples=1000] ({-pow(1-\y*\y, (1/3))},{\y});
\draw[thick, domain=1:2.2, smooth, variable=\y, blue] plot [samples=500] ({pow(\y*\y -1, (1/3))},{\y});
\end{tikzpicture}
\end{document}
这让我想到了 我试图通过故意稍微超越界限来缩小可见的差距,通过写作
\draw[thick, domain=-2.2:-0.99, smooth, variable=\y, blue] plot [samples=500] ({pow(\y*\y -1, (1/3))},{\y});
\draw[thick, domain=-1:1.01, smooth, variable=\y, blue] plot [samples=1000] ({-pow(1-\y*\y, (1/3))},{\y});
\draw[thick, domain=1:2.2, smooth, variable=\y, blue] plot [samples=500] ({pow(\y*\y -1, (1/3))},{\y});
这给出了一个视觉上令人满意的图表,但可以理解的是,它会导致一些 PGF Math 错误,抱怨我试图在少数(多余的)情况下计算负数的对数。我想使用该silence
包来抑制这些错误,因为它们不会造成真正的伤害。但是,\ErrorsOff[tikz,pgf,pgfplots]
无法关闭错误。任何让我知道如何关闭错误的答案或建议一种更优雅的绘制曲线的方法都将不胜感激。
答案1
您可以定义一个函数来避免对负数取三次方根。对于您的情况,您需要-sign(1-\y*\y)*pow(abs(1-\y*\y),1/3)
。此函数可以在完整域中绘制。
\documentclass[tikz,border=2mm]{standalone}
\begin{document}
\begin{tikzpicture}[declare function={f(\y)=-sign(1-\y*\y)*pow(abs(1-\y*\y),1/3);}]
\draw[->] (-3,0) -- (3,0) node[right] {$x$};
\draw[->] (0,-3) -- (0,3) node[above] {$y$};
\draw[thick, domain=-2.2:2.2, smooth, variable=\y, blue]
plot [samples=101] ({f(\y)},{\y});
\end{tikzpicture}
\end{document}