tikz 无法正确绘图

tikz 无法正确绘图

@Jake:这是整个文档

    \documentclass[11pt, a4paper]{book}
    \usepackage{amssymb}
    \usepackage{amsmath}
    \usepackage{amsthm}
    \usepackage{tikz}
    \usepackage{pgfplots}
    \usepgflibrary{arrows}
    \usepackage{pgf}
    \usepackage{Tikz-exp}


    \parindent=0pt

    \begin{document}

    \begin{tikzpicture}
    \tikzset{x={(.965cm,-.258cm)}, y={(.7cm,.7cm)}, z={(0cm,1cm)},
    sezioni/.style={draw=#1!60, thin, fill=#1!30, fill opacity=0.3},
    scale=1.75
    }
    \coordinate (O) at (0,0,0) node [left] {$O$};
    \draw[gray,->] (-2,0,0) -- (2,0,0) node [right, black] {$x$};
    \draw[gray, ->] (0,-2,0) -- (0,2,0) node [right, black] {$y$};
    \draw[gray, ->] (O) -- (0,0,3) node [left, black] {$z$};

    %=====% sezioni %=====%
    \foreach \x in {-1,-.85,...,1}
        \draw[sezioni=teal] (\x,{sqrt(1-(\x)^2)},0)--(\x,0,{sqrt(4*(1-(\x)^2))})--(\x,{-sqrt(1-(\x)^2)},0)--(\x,{sqrt(1-(\x)^2)},0);
    %=====================%

    \draw[blue] (O) circle (1);
    \draw[red, domain=-1:1, samples=500] plot (\x,0,{sqrt(4*(1-(\x)^2))});
    \end{tikzpicture}



    \end{document}

x=1 中的红色映射应该产生值零,但它是正数。

答案1

这是由于确定在哪里对函数进行采样时数值不准确造成的。以下代码确保始终对函数的上限进行domain采样:

\documentclass{article}
\usepackage{tikz}

\makeatletter
\def\tikz@plot@samples@recalc#1:#2\relax{%
  \pgfmathparse{#1}%
  \let\tikz@temp@start=\pgfmathresult%
  \pgfmathparse{#2}%
  \let\tikz@temp@end=\pgfmathresult%
  \pgfmathparse{\tikz@temp@start+(\tikz@temp@end-\tikz@temp@start)/(\tikz@plot@samples-1)}%
  \edef\tikz@plot@samplesat{\tikz@temp@start,\pgfmathresult,...,\tikz@temp@end,\tikz@temp@end}%
}
\makeatother


\begin{document}
    \begin{tikzpicture}
    \draw (-2,0,0) -- (2,0,0);

    \draw[red, domain=-1:1, samples=50] plot (\x,{sqrt(4*(1-(\x)^2))});
    \end{tikzpicture}
\end{document}

相关内容