功能
f(x) = \frac{x^4}{12}-\frac{2x^3}{3}+\frac{3 x^2}{2}
有两个拐点x = 1
和x=3
。我手工绘制了上述点处的切线。
\documentclass[tikz,border=3.14mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\begin{document}
\begin{tikzpicture}[>=latex]
\begin{axis}[
xtick={-2,-1,...,5},
xticklabel=\empty,
ytick={-2,-1,...,5},
yticklabel=\empty, grid=major,
axis x line=center,
axis y line=center,
xlabel={$x$},
ylabel={$y$},
xlabel style={below},
ylabel style={left},
xmin=-2,
xmax=5,
ymin=-2,
ymax=5]
\addplot[color=blue,smooth,samples=501, thick,domain=-5:5] {(3* x^2)/2 - (2 *x^3)/3 + x^4/12};
\addplot[color=purple,smooth,samples=501, thick,domain=-5:5] {-(5/12) + (4 *x)/3};
\addplot[color=red,smooth,samples=501, thick,domain=-5:5] {9/4};
\end{axis}
\end{tikzpicture}
\end{document}
如何自动绘制每个函数拐点处的切线
答案1
这只是为了好玩。它确实以合理的精度在拐点处产生了切线。Ti钾Z 根据输入函数计算它们的位置。
\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\makeatletter
\def\parsenode[#1]#2\pgf@nil{%
\tikzset{label node/.style={#1}}
\def\nodetext{#2}
}
\tikzset{
add node at x/.style 2 args={
name path global=plot line,
/pgfplots/execute at end plot visualization/.append={
\begingroup
\@ifnextchar[{\parsenode}{\parsenode[]}#2\pgf@nil
\path [xshift=-0.1pt,name path global = position line #1-1]
({axis cs:#1,0}|-{rel axis cs:0,0}) --
({axis cs:#1,0}|-{rel axis cs:0,1});
\path [xshift=0.1pt, name path global = position line #1-2]
({axis cs:#1,0}|-{rel axis cs:0,0}) --
({axis cs:#1,0}|-{rel axis cs:0,1});
\path [
name intersections={
of={plot line and position line #1-1},
name=left intersection
},
name intersections={
of={plot line and position line #1-2},
name=right intersection
},
label node/.append style={pos=1}
] (left intersection-1) -- (right intersection-1)
node [label node]{\nodetext};
\endgroup
}
}
}
\pgfplotsset{tangent/.style={
add node at x={#1}{
[
sloped,
append after command={(\tikzlastnode.west) edge [thick,black] (\tikzlastnode.east)},
minimum width=0.2\textwidth
]
}
}}
\makeatother
\begin{document}
\begin{tikzpicture}[>=latex,declare function={f(\x)=(3* pow(\x,2))/2 -
(2 *pow(\x,3))/3 + pow(\x,4)/12;difff(\x)=(f(\x+0.02)-f(\x-0.02))/0.04;
ddifff(\x)=(difff(\x+0.02)-difff(\x-0.02))/0.04;}]
\begin{scope}[opacity=0,overlay]
\draw[name path=ddiff] plot[variable=\x,domain=-2:5] (\x,{ddifff(\x)});
\draw[name path=xaxis] (-2,0) -- (5,0);
\path[name intersections={of={ddiff and xaxis},total=\t}]
foreach \X in {1,...,\t} {let \p1=(intersection-\X) in
\pgfextra{\pgfmathparse{\x1/1cm}
\ifnum\X=1
\xdef\LstInfPts{\pgfmathresult}
\else
\xdef\LstInfPts{\LstInfPts,\pgfmathresult}
\fi}};
\end{scope}
\begin{axis}[
xtick={-2,-1,...,5},
xticklabel=\empty,
ytick={-2,-1,...,5},
yticklabel=\empty, grid=major,
axis x line=center,
axis y line=center,
xlabel={$x$},
ylabel={$y$},
xlabel style={below},
ylabel style={left},
xmin=-2,
xmax=5,
ymin=-2,
ymax=5]
\addplot[color=blue,smooth,samples=501, thick,domain=-5:5,
tangent/.list/.expanded=\LstInfPts] {f(x)};
\end{axis}
\end{tikzpicture}
\end{document}