在评估 PGF 数学宏中声明的函数时,绘图显示“除以零”

在评估 PGF 数学宏中声明的函数时,绘图显示“除以零”

我最初用 Geogebra 构建了这个图,但我对结果的样子并不满意(如果其他一切都失败了,我当然会使用它;但我想了解这里的错误是什么)。问题出在这里标记为“龙”的代码(之后\end{scriptsize}):

\documentclass[10pt,tikz]{standalone}
% \draw let {conds commas} draw-instr
\usetikzlibrary{arrows,angles,intersections,calc}
\begin{document}
\definecolor{ffqqtt}{rgb}{1,0,0.2} %red
\definecolor{qqqqzz}{rgb}{0,0,0.6} %blue
\definecolor{qqzzcc}{rgb}{0,0.6,0.8} %cyan
\definecolor{fftttt}{rgb}{1,0.2,0.2}
\definecolor{uququq}{rgb}{0.25,0.25,0.25}
\definecolor{xdxdff}{rgb}{0.49,0.49,1}
\begin{tikzpicture}[
    line cap=round,
    line join=round,
    >=triangle 45,
    x=3cm,
    y=3cm,
    declare function = {
        f(\x) = sqrt(1-(\x)^2);
        fp(\x) = \x/(f(\x));
    }
]
\coordinate (o) at (0,0);
\path[clip] (-3.4,-0.13) rectangle (0.35,1.2);
\draw[%%X AXIS
    <-,
    color=black,
    name path=xaxis
] (-3.4,0) -- (0.3,0);
%%Y AXIS
\draw[->,color=black] (0,-0.01) -- (0,1.18);
%%PLOT
\draw[
    thick, 
    green!80!black,
    smooth,
    samples=100,
    domain=-1:0.15
] plot(\x,{f(\x)});
\begin{scriptsize}
\def\a{ -0.46}
\def\aph{ -0.07}
\def\amh{ -0.85}
\fill [color=xdxdff] (\a,0) coordinate[label=below:$a$] (a) circle (1.5pt);
\fill [color=uququq] (\a,{f(\a)}) coordinate[label=below:$b$] (b) circle (1.5pt);
\fill [color=xdxdff] (\aph,0) coordinate[label=below:$a+h$] (aph) circle (1.5pt);
\fill [color=xdxdff] (\amh,0) coordinate[label=below:$a+h$] (amh) circle (1.5pt);
\fill [color=uququq] (\amh,{f(\amh)}) coordinate (A) circle (1.5pt);
\fill [color=uququq] (\aph,{f(\aph)}) coordinate (B) circle (1.5pt);
\end{scriptsize}
%%%%%%%HERE BE THE DRAGON!!!!
\draw[
    red,
    thick,
    domain=-3.4:0.3,
    samples=850,
    name path=redtan
] let \p1 = (a) in
\pgfextra{
    \pgfmathsetmacro{\mred}{
        fp(\x1)*(\x - \x1) + fp(\x1)
    }
    \pgfmathsetmacro{\bred}{
        f(\x1) - \mred*\x1
    }
}
plot (\x,{\mred*(\x - \x1) + \bred});
%%%/DRAGON

%ANGLES
%\node[
%   label=270:$f(a-h)$
%] at (A) {};
%\path[name intersections={of=xaxis and redtan,by=re}];
%%\coordinate (R) at (re);
%\path[name intersections={of=xaxis and bluetan,by=bl}];
%%\coordinate (L) at (bl);
%\path[name intersections={of=xaxis and cyantan,by=cy}];
%%\coordinate (C) at (cy) ;
%\path pic[fill=qqzzcc] {angle = o--cy--A}; %CYAN ANGLE
%\path pic[fill=ffqqtt] {angle = o--re--b}; %RED ANGLE
%\path pic[fill=qqqqzz] {angle = o--bl--B}; %BLUE ANGLE

%TRIANGLE
\draw[
    thick,
    densely dotted,
    orange
] let \p1=(A), \p2=(B) in (A) -- (\x2,\y1) coordinate (ra) -- (B) -- cycle; 
%%PARALLELS TO Y AXIS TRIANGLE
\draw[
    thin,
    gray,
    densely dotted
] (amh) -- (A);
\draw[
    thin,
    gray,
    densely dotted
] (aph) -- (ra);
%%FUNC NAME
\node at (-0.4,1) [anchor=south east]
 {\color{green!80!black}{$y=\sqrt{1-x^2}$}};
\end{tikzpicture}
\end{document}

我得到的错误是Package PGF Math Error: I cannot calculate the square-root of the negative number '-1540.76892' (in 'sqrt(1-(-39.26537)^2)'). }。我还注意到了这一点:Paragraph ended before \pgfmath@dimen@@ was complete.,据我理解,这意味着在某个时刻pgf停止了计算维度。我不明白为什么我会得到这个错误!我甚至可以绘制函数fp(它是 的导数f)。几个小时后,我唯一的猜测是我在 a 内部使用声明函数\pgfmathsetmacro是错误的;然而,当我在“龙穴”中尝试以下操作时也发生了这个错误:

\draw[
    red,
    thick,
    domain=-3.4:0.3,
    samples=850,
    name path=redtan
] let \p1 = (a) in plot (\x,{fp(\x1)*(\x - \x1) + f(\x1)});

我也尝试过直接使用定义为点的 x 坐标的宏\a,但仍然出现上述错误。请帮忙!谢谢!:)

答案1

问题似乎在于我在环境\a内部进行了定义scriptsize;我删除了该let指令并进行了一些优化。结果如下:

\documentclass[10pt,tikz]{standalone}
\usetikzlibrary{
    arrows,
    angles,
    intersections,
    calc,
    positioning
}
\begin{document}
\definecolor{ffqqtt}{rgb}{1,0,0.2} %red
\definecolor{qqqqzz}{rgb}{0,0,0.6} %blue
\definecolor{qqzzcc}{rgb}{0,0.6,0.8} %cyan
\definecolor{fftttt}{rgb}{1,0.2,0.2}
\definecolor{uququq}{rgb}{0.25,0.25,0.25}
\definecolor{xdxdff}{rgb}{0.49,0.49,1}
\begin{tikzpicture}[
    line cap=round,
    line join=round,
    >=triangle 45,
    x=3cm,
    y=3cm,
    declare function = {
        f(\x) = sqrt(1-(\x)^2);
        fp(\x) = -\x/(f(\x));
        eme(\x,\y) = (f(\x)-f(\y))/(\x - \y);
        be(\x,\y) = f(\y) - eme(\x,\y)*\y;
    }
]
\def\exmin{ -3.7}
\def\a{ -0.46}
\def\aph{ -0.07}
\def\amh{ -0.85}
\coordinate (o) at (0,0);
\path[clip] (\exmin,-0.13) rectangle (0.35,1.2);
\draw[%%X AXIS
    <-,
    color=black,
    name path=xaxis
] (\exmin,0) -- (0.3,0);
%%Y AXIS
\draw[->,color=black] (0,-0.01) -- (0,1.18);
%%PLOT
\draw[
    very thick, 
    green!80!black,
    smooth,
    samples=100,
    domain=-1:0.15
] plot(\x,{f(\x)});
\begin{scriptsize}
\fill [color=xdxdff] (\a,0) coordinate[label=below:$a$] (a) circle (1.5pt);
\fill [color=uququq] (\a,{f(\a)}) coordinate (b) circle (1.5pt);
\fill [color=xdxdff] (\aph,0) coordinate[label=below:$a+h$] (aph) circle (1.5pt);
\fill [color=xdxdff] (\amh,0) coordinate[label=below:$a-h$] (amh) circle (1.5pt);
\fill [color=uququq] (\amh,{f(\amh)}) coordinate (A) circle (1.5pt);
\node[
    fill=gray!50, 
    fill opacity=0.7,
    below = 5pt of A
] {$f(a-h)$}; 
\fill [color=uququq] (\aph,{f(\aph)}) coordinate (B) circle (1.5pt);
\node[
fill=gray!50, 
fill opacity=0.7,
below = 5pt of B
] {$f(a+h)$}; 
\node[
fill=gray!50, 
fill opacity=0.7,
below = 1.5pt of b
] {$f(a)$}; 
\end{scriptsize}
\tikzset{domain=\exmin:0.3}
\draw[
    ffqqtt,
    thick,
    samples=100,
    name path=redtan
] plot (\x,{fp(\a)*(\x-\a) + f(\a)});
\draw[
    qqqqzz,
    densely dashed,
    samples=100,
    name path=bluetan
] 
plot (\x,{eme(\aph,\a)*\x + be(\aph,\a)});
\draw[
    qqzzcc,
    densely dashed,
    samples=100,
    name path=cyantan
] 
plot (\x,{eme(\a,\amh)*\x + be(\a,\amh)});
%ANGLES
\path[name intersections={of=xaxis and redtan,by=re}];
%\coordinate (R) at (re);
\path[name intersections={of=xaxis and bluetan,by=bl}];
%\coordinate (L) at (bl);
\path[name intersections={of=xaxis and cyantan,by=cy}];
%\coordinate (C) at (cy) ;
\path pic[fill=qqzzcc] {angle = o--cy--A}; %CYAN ANGLE
\path pic[fill=ffqqtt] {angle = o--re--b}; %RED ANGLE
\path pic[fill=qqqqzz] {angle = o--bl--B}; %BLUE ANGLE

%TRIANGLE
\draw[
    thick,
    densely dotted,
    orange
] let \p1=(A), \p2=(B) in (A) -- (\x2,\y1) coordinate (ra) -- (B) -- cycle; 
%%PARALLELS TO Y AXIS TRIANGLE
\draw[
    thin,
    gray,
    densely dotted
] (amh) -- (A);
\draw[
    thin,
    gray,
    densely dotted
] (aph) -- (ra);
%%FUNC NAME
\node at (-0.4,1) [anchor=south east]
 {\color{green!80!black}{$y=\sqrt{1-x^2}$}};
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容