2

I originally constructed this plot with Geogebra, but I wasn't satisfied with the way the result looked like (if everything else fails, I'll use it, sure; but I want to understand what's the error here). The problem is with the code labeled here as a "dragon" (after \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}

The error I get is Package PGF Math Error: I cannot calculate the square-root of the negative number '-1540.76892' (in 'sqrt(1-(-39.26537)^2)'). }. I also noticed this one: Paragraph ended before \pgfmath@dimen@@ was complete., which as I understand means that at some point pgf stopped computing a dimension. I do not understand why I get this error! I can even plot the function fp (which is the derivative of f). After several hours my only guess is that my using of declared functions inside a \pgfmathsetmacro is faulty; however, this error also occurred when I tried the following in the "dragon's lair":

\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)});

I've also tried using the macro \a defined directly as the x coordinate of a point, but I still get the same errors above. Please help! Thanks! :)

0

1 Answer 1

2

The problem seems to have been the fact that I defined \a inside the scriptsize environment; I got rid of the let instruction and optimised a bit. Here's the result:

\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}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .