我有以下代码来构建给定函数的导数:
\documentclass[tikz, border=.5cm]{standalone}
\usepackage{tkz-fct}
\usepackage{xfp}
\usetikzlibrary{spy}
\definecolor{vinho}{rgb}{0.0, 0.18, 0.39}
\definecolor{vermelho}{rgb}{0.93, 0.11, 0.14}
\begin{document}
\def\a{2}
\foreach \b in {2.5,2.45,...,2.3,2.2,2.1,2.09,...,2.01}{
\begin{tikzpicture}[scale=2,cap=round,declare function = {
f(\x) = .5*(\x-1.5)*(\x-1.5)+1;},
spy using outlines={circle,magnification=2, height=0.5cm,size=1.5cm, connect spies}]
\clip (-1.8,-0.8) rectangle (5,3.5);
\draw[-latex] (-0.5,0) -- (4,0) node[right]{$x$};
\draw[-latex] (0,-0.5) -- (0,3) node[above]{$y$};
\draw[vinho,thick,domain=1:3.5,samples=200] plot(\x,{f(\x)}) node[right]{$f$};
\draw[vermelho,thick,domain=-1:1.91,samples=200] plot({\a + \x*(\b-\a)/(sqrt( (\b-\a)^2 + (f(\b)-f(\a))^2 ))},{f(\a) + \x*(f(\b)-f(\a))/(sqrt( (\b-\a)^2 + (f(\b)-f(\a))^2 ))}) node[right]{$s$};
\draw[densely dotted] (0,{f(\b)}) -- (\b,{f(\b)}) -- (\b,0) node[below]{$x_0+h$};
\draw[densely dotted] (0,{f(\a)}) -- (\a,{f(\a)}) -- (\a,0) node[below=0.08cm]{$x_0$};
\draw[fill] (\a,{f(\a)}) node[above=0.3cm]{$P$} circle (1pt);
\draw[fill] (\b,{f(\b)}) node[above]{$Q$} circle (1pt);
\draw[decoration={brace, raise=10pt},decorate,vinho!50] (0,{f(\a)}) -- node[left=10pt] {$f(x_0+h)-f(x_0)$} (0,{f(\b)});
\draw[decoration={brace,mirror,raise=5pt},decorate,vinho!50] ({\a},-.250) -- node[below=6pt] {$h$} ({\b},-.250);
\spy on ({2*\a},{2*f(\a)}) in node at (0.7,0.7);
\end{tikzpicture}
}
\end{document}
对于循环中的每个 \b,我构建一个不同的框架。
不过,我想画个间谍:
\spy on ({2*\a},{2*f(\a)}) in node at (0.7,0.7);
仅适用于 b < 2.1。处理非整数时,Tikz 不接受条件。
我怎样才能做到这一点?
答案1
你可以应用最后一个解决方案这个答案这里:
表达式int(ifthenelse(\b<2.1, 1, 0))
将输出1
或0
(两个整数),具体取决于是否\b
小于 2.1。
此解决方案规避了无法在 Ti 中实现非整数条件的问题钾Z 直接。它利用ifthenelse
PGF 自带的函数将非整数转换为 0 或 1,然后可以轻松地用 进行测试\ifnum
。
(为了减少编译时间,我减少了样本大小。)
\documentclass[tikz, border=.5cm]{standalone}
\usepackage{tkz-fct}
\usepackage{xfp}
\usetikzlibrary{spy}
\definecolor{vinho}{rgb}{0.0, 0.18, 0.39}
\definecolor{vermelho}{rgb}{0.93, 0.11, 0.14}
\begin{document}
\def\a{2}
\foreach \b [evaluate=\b as \z using {int(ifthenelse(\b<2.1, 1, 0))}] in {2.2, 2.1, 2.09, 2.08} {
\begin{tikzpicture}[scale=2, cap=round, declare function={
f(\x) = .5*(\x - 1.5)*(\x - 1.5) + 1;
}, spy using outlines={circle, magnification=2, height=0.5cm, size=1.5cm, connect spies}]
\clip (-1.8,-0.8) rectangle (5,3.5);
\draw[-latex] (-0.5,0) -- (4,0) node[right] {$x$};
\draw[-latex] (0,-0.5) -- (0,3) node[above] {$y$};
\draw[vinho, thick, domain=1:3.5, samples=200] plot(\x, {f(\x)}) node[right] {$f$};
\draw[vermelho, thick, domain=-1:1.91, samples=200] plot({\a + \x*(\b - \a)/(sqrt((\b - \a)^2 + (f(\b) - f(\a))^2))}, {f(\a) + \x*(f(\b) - f(\a))/(sqrt((\b - \a)^2 + (f(\b) - f(\a))^2))}) node[right] {$s$};
\draw[densely dotted] (0,{f(\b)}) -- (\b,{f(\b)}) -- (\b,0) node[below] {$x_0 + h$};
\draw[densely dotted] (0,{f(\a)}) -- (\a,{f(\a)}) -- (\a,0) node[below=0.08cm] {$x_0$};
\draw[fill] (\a,{f(\a)}) node[above=0.3cm] {$P$} circle (1pt);
\draw[fill] (\b,{f(\b)}) node[above] {$Q$} circle (1pt);
\draw[decoration={brace, raise=10pt}, decorate, vinho!50] (0,{f(\a)}) -- node[left=10pt] {$f(x_0 + h) - f(x_0)$} (0,{f(\b)});
\draw[decoration={brace, mirror, raise=5pt}, decorate, vinho!50] ({\a},-0.250) -- node[below=6pt] {$h$} ({\b},-0.250);
\ifnum \z=1
\spy on ({2*\a},{2*f(\a)}) in node at (0.7,0.7);
\fi
\end{tikzpicture}
}
\end{document}