Tikz sqrt函数不绘制sqrt(0)

Tikz sqrt函数不绘制sqrt(0)

我正在尝试绘制一个涉及的函数sqrt,但我不明白为什么它直到 sqrt(0) 才绘制。

蓝色曲线应该在 x=8 处达到 y=0,但它在此之前就停止了,而红色曲线则不会发生这种情况。有人知道如何强制蓝色曲线绘制在 x=0 和 x=8 之间的整个域上吗?

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

    \begin{tikzpicture}

    \begin{axis}[
    scale only axis, % To ensure same size on all pictures axis
    restrict y to domain=-12:12,
    axis x line=center,
    axis y line=center,
    ticks = none,
    samples=150]

    \addplot [no markers] coordinates {(12,10)}; % To maintain scale at size without ticks

    % Plot curves
    \addplot[blue, very thick, domain=0:8]{sqrt(20/2.8*(8-x))};
    \addplot[red, very thick, domain=0:9.5]{sqrt(20/2.8*(11-x))-1.3};
    \addplot[red, very thick] coordinates {(9.5,0) (9.5,1.98)};

    \end{axis}

    \end{tikzpicture}

\end{document}

答案1

垂直线不会\addplot coordinates ....引起问题。蓝色图不完整的原因是存在舍入误差。将平方根的参数包裹在中即可abs

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

    \begin{tikzpicture}

    \begin{axis}[
    scale only axis, % To ensure same size on all pictures axis
    restrict y to domain=-12:12,
    axis x line=center,
    axis y line=center,
    ticks = none,
    samples=150]

    \addplot [no markers] coordinates {(12,10)}; % To maintain scale at size without ticks

    % Plot curves
    \addplot[blue, very thick, domain=0:8]{sqrt(abs(20/2.8*(8-x)))};
    \addplot[red, very thick, domain=0:9.5]{sqrt(20/2.8*(11-x))-1.3};
    \addplot[red, very thick] coordinates {(9.5,0) (9.5,1.98)};

    \end{axis}

    \end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

改用简单的图来绘制垂直线段。垂直线的斜率为,±inf这可能会导致 pgfplots 出现问题。

\documentclass[border=5pt]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\begin{document}

    \begin{tikzpicture}

    \begin{axis}[
    scale only axis, % To ensure same size on all pictures axis
    restrict y to domain=-12:12,
    axis x line=center,
    axis y line=center,
    %ticks = none,
    samples=150]

    \addplot [no markers] coordinates {(12,10)}; % To maintain scale at size without ticks

    % Plot curves
    \addplot[blue, very thick, domain=0:8]{sqrt(20/2.8*(8-x))};
    \addplot[red, very thick, domain=0:9.5]{sqrt(20/2.8*(11-x))-1.3};
    %\addplot[red, very thick] coordinates {(9.5,0) (9.5,1.98)};
    \draw [red, very thick](9.5,0) -- (9.5,1.98);

    \end{axis}

    \end{tikzpicture}

\end{document}

在此处输入图片描述

编辑:

提高准确度的另一种方法是增加样本数量,这样曲线看起来会更好。

\addplot[blue, very thick, samples=1000, domain=0:8,]{sqrt(20/2.8*(8-x))};

在此处输入图片描述

相关内容