我想\draw
结合相对坐标使用该命令来绘制图形切线下方的斜率三角形,但以下语法无法产生所需的结果:
\draw [color=red] (-1,4) -- ++(0,-1) -- ++(0.5,0);
相反,我得到的结果如下:
完整代码如下:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage[ngerman]{babel}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{positioning,intersections,shapes.geometric,automata,math}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=newest}
\usepgfplotslibrary{external}
\tikzexternalize
\begin{document}
\begin{tikzpicture}
[declare function={
f(\x) = \x^3 - 5*\x;
derf(\x) = 3*\x^2 - 5;
g(\x,\y) = derf(\y)*(\x-\y) + f(\y);
}]
\begin{axis}[
axis x line=center,
axis y line=center,
xtick=\empty,
ytick=\empty,
xlabel={$x$},
ylabel={$y$},
xlabel style={below right},
ylabel style={above left},
xmin=-3.5,
xmax=3.5,
ymin=-15,
ymax=15
]
\addplot [domain=-3:3,smooth,thick, color=black, name path=p1] {f(x)} node[above right, xshift=-9mm] {$y=f(x)$};
\tikzmath{\z1=-2.6;}
\addplot [only marks,mark=*] coordinates { (\z1,{f(\z1)}) };
\addplot [domain=-3.1:-2.1, color=black] { g(x,\z1) };
\draw [dashed] (\z1,0) -- (\z1,{f(\z1)});
\draw (\z1,2pt) -- (\z1,-2pt) node[above] {$x_1$};
\tikzmath{\z2=-1;}
\addplot [only marks,mark=*] coordinates { (\z2,{f(\z2)}) };
\addplot [domain=-2.3:0.3, color=black] { g(x,\z2) };
\draw [dashed] (\z2,0) -- (\z2,{f(\z2)});
\draw (\z2,2pt) -- (\z2,-2pt) node[below] {$x_2$};
\draw [color=red] (-1,4) -- ++(0,-1) -- ++(0.5,0);
\tikzmath{\z3=1.8;}
\addplot [only marks,mark=*] coordinates { (\z3,{f(\z3)}) };
\addplot [domain=0.8:2.8, color=black] { g(x,\z3) };
\draw [dashed] (\z3,0) -- (\z3,{f(\z3)});
\draw (\z3,2pt) -- (\z3,-2pt) node[above] {$x_3$};
\end{axis}
\end{tikzpicture}
\end{document}
我对 Tikz 和 Pgfplots 还很陌生,也许我只是犯了一个愚蠢的错误。无论如何,如果有人能帮助我解决这个问题,我将不胜感激。
除此之外,我还非常感激任何改进代码、简化代码和尽可能清理代码的建议或意见。例如,我想知道是否可以用一个代码块替换绘制切线的三个代码块,使用一个公共变量取三个不同的值,而不是\z1, \z2, \z3
分别定义三个变量。
提前谢谢了!
答案1
您的代码无法编译,因此我只提供一行解决方案,而不是可编译的代码:
\draw [red] (-1,4) -- ++(axis direction cs:0,-1) -- ++(axis direction cs:0.5,0); %ok now
https://tex.stackexchange.com/a/332603/8650
编辑:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{arrows.meta}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\begin{document}
\begin{tikzpicture}[
declare function={
f(\x) = \x^3 - 5*\x;
derf(\x) = 3*\x^2 - 5;
g(\x,\xzero) = derf(\xzero)*(\x-\xzero) + f(\xzero);
x1=-2.6;
x2=-1;
x3=1.8;
}]
\begin{axis}[
axis x line=center, axis y line=center,
xtick=\empty, ytick=\empty,
xlabel={$x$}, ylabel={$y$},
xlabel style={below right}, ylabel style={above left},
xmin=-3.5, xmax=3.5,
ymin=-15, ymax=15,
clip=false,
xtick={x1, x2, x3}, xticklabel=\empty
]
\draw[dashed] (x1,0) node[above] {\footnotesize $x_1$} -- (x1,{f(x1)});
\addplot[domain=-3.1:-2.1] { g(x,x1) };
\draw plot[mark=*, mark size=1.5pt] coordinates { (x1,{f(x1)}) };
\draw[dashed] (x2,0) node[below] {\footnotesize $x_2$} -- (x2,{f(x2)});
\draw [red] (x2,{f(x2)}) -- ++(axis direction cs:0,-1) -- ++(axis direction cs:{-1/derf(x2)},0);
\addplot[domain=-1.8:-0.2] { g(x,x2) };
\draw plot[mark=*, mark size=1.5pt] coordinates { (x2,{f(x2)}) };
\draw[dashed] (x3,0) node[above] {\footnotesize $x_3$} -- (x3,{f(x3)});
\addplot[domain=1.2:2.3] { g(x,x3) };
\draw plot[mark=*, mark size=1.5pt] coordinates { (x3,{f(x3)}) };
\addplot [domain=-3:3,smooth,thick] {f(x)} node[above] {\footnotesize $y=f(x)$};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
为了好玩,带有 的代码pstricks
定义了一个\psplotTangent
命令:
\documentclass[svgnames, border=6pt]{standalone}
\usepackage{pstricks-add}%
\begin{document}
\psset{linejoin=1, arrowinset=0.12, yunit=0.25cm, labelsep=3pt}
\begin{pspicture*}(-3.5,-15)(3.5,15)
\psaxes[ticks=none, labels=none]{->}(0,0)(-3.5,-15)(3.5,15)[$x$,-110][$y$,-135]%
\psplot[linewidth=1.2pt, linecolor=RoyalBlue, plotpoints=500, plotstyle=curve, algebraic]{-3.5}{3.5}{x^3-5*x}
\foreach \x in {-2.6,-1,1.8}{\psplotTangent[algebraic, linewidth=0.6pt]{\x}{2}{x^3-5*x}}
\foreach \x/\y in {-2.6/-4.58,-1/4,1.8/-3.17}{\psset{linestyle=dashed, dash=3pt 2pt, linewidth=0.4pt} \psline{*-}(\x, \y)(\x, 0)}
\uput[u](-2.6, 0){\footnotesize$-2.6$}\uput[d](-1, 0){\footnotesize$-1$}%
\uput[u](1.8, 0){\footnotesize$1.8$}
\psline[linecolor=IndianRed](0,2)(-1,2)
\end{pspicture*}
\end{document}
答案3
使用tzplot
包裹:
\documentclass[tikz]{standalone}
\usepackage{tzplot}
\begin{document}
\begin{tikzpicture}[yscale=.2]
%\tzhelplines(-4,-15)(4,15)
\tzaxes*(-4,-15)(4,15){$x$}[br]{$y$}[al]
\def\Fx{(\x)^3-5*\x}
\tzfn[blue,thick]\Fx[-3:3]{$y=f(x)$}[a,black]
% tangent
\tzvXpointat*{Fx}{-2.6}(x1)
\tzvXpointat*{Fx}{-1} (x2)
\tzvXpointat*{Fx}{1.8} (x3)
\tztangent[red] {Fx}(x1)[-3.1:-2.1]
\tztangent[red]"tan"{Fx}(x2)[-2.1:0.2]
\tztangent[red] {Fx}(x3)[1:2.6]
% projection
\tzprojx(x1){$x_1$}[a]
\tzprojx(x2){$x_2$}
\tzprojx(x3){$x_3$}[a]
% slope triangle
\tzvXpointat{tan}{-.5}(x2a)
\tzlink[red](x2)[|-](x2a)
\end{tikzpicture}
\end{document}