我正在尝试绘制函数y = √{x 2 - 0.5},如图所示:
我的代码如下:
\begin{tikzpicture}
\begin{axis}[
xmin=-3, xmax=3,
ymin=-3, ymax=3,
axis lines = center,
xtick={-3,-2.5,...,2.5,3},
ytick={-3,-2.5,...,2.5,3},
xticklabel style = {font=\tiny,yshift=0.5ex},
yticklabel style = {font=\tiny,xshift=0.5ex}
]
\addplot [mark=none, unbounded coords=jump] {sqrt(x^2 - 0.5)};
\end{axis}
\end{tikzpicture}
这几乎可以正常工作,但是产生了这种奇怪的不连续性:
我unbounded coords=jump
自己添加了该部分,否则只有一条水平线连接这两个底部端点。为什么这不会继续显示 y = ~0.4 以下函数的输出?任何帮助都将不胜感激。提前致谢。
答案1
平方根在域 中有实数值]-∞,-√.5] U [√.5,∞[
,因此您需要分别绘制两个子域。abs()
对根取绝对值将保证输出为实数。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-3, xmax=3,
ymin=-3, ymax=3,
axis lines = center,
xtick={-3,-2.5,...,2.5,3},
ytick={-3,-2.5,...,2.5,3},
xticklabel style = {font=\tiny,yshift=0.5ex},
yticklabel style = {font=\tiny,xshift=0.5ex}
]
\addplot [samples=300,domain=sqrt(.5):3]{sqrt(abs(x^2 - 0.5))};
\addplot [samples=300,domain=-3:-sqrt(.5)]{sqrt(abs(x^2 - 0.5))};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
您的样本数量有问题。y =0 你实际上需要无穷多...尝试一下,如果以下解决方案接近你想要的:
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usepackage[active,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{1em}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=-3, xmax=3,
ymin=-3, ymax=3,
axis lines = center,
xtick={-3,-2.5,...,3},
ytick={-3,-2.5,...,3},
xticklabel style = {font=\tiny,yshift=0.5ex},
yticklabel style = {font=\tiny,xshift=0.5ex}
mark=none, unbounded coords=jump,
]
\addplot [blue, very thick,
samples=500, domain=-1:1] {sqrt(x^2 - 0.5)};
\addplot [blue, very thick,, smooth,
samples=10, domain=-3:3] {sqrt(x^2 - 0.5)};
\end{axis}
\end{tikzpicture}
\end{document}