我正在尝试复制一个图表,并有一个最小示例。我不知道如何在指定点之间添加额外的切线并保持平滑度。
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\begin{document}
\begin{tikzpicture}
% Axes
\draw [->, name path=x] (-1,0) -- (11,0) node [right] {$x$};
\draw [->] (0,-1) -- (0,6) node [above] {$y$};
% Origin
\node at (0,0) [below left] {$0$};
% Points
\coordinate (start) at (1,-0.8);
\coordinate (c1) at (3,3);
\coordinate (c2) at (5.5,1.5);
\coordinate (c3) at (8,4);
\coordinate (end) at (10.5,-0.8);
% show the points
\foreach \n in {start,c1,c2,c3,end} \fill [black] (\n)
circle (2pt) node [below] {};
% join the coordinates
\draw [thick,name path=curve] (start) to[out=70,in=180] (c1) to[out=0,in=180]
(c2) to[out=0,in=180] (c3) to[out=0,in=150] (end);
% add tangets and dashed lines
\foreach \c in {1,2,3} {
%\draw [dashed] let \p1=(c\c) in (c\c) -- (\x1,0) node [below] {$c_\c$};
\draw ($(c\c)-(0.75,0)$) -- ($(c\c)+(0.75,0)$) node [midway,above=4mm]{$$}; %{$f'(c_\c)=0$};
}
% add a and b
\path [name intersections={of={x and curve}, by={a,b}}] (a) node [below left] {$a$}
(b) node [above right] {$b$};
\end{tikzpicture}
\end{document}
输出:
随意进行任何更改,使最小示例接近目标图
答案1
你可以使用一些微积分:
该\DrawTangent
宏在给定的 x 值周围创建两条垂直线,并计算它们与曲线的交点。然后,可以用这两个交点之间的正割来近似计算切线。
代码:
\documentclass[border=2pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\newcommand*{\DeltaX}{0.01}
\newcommand*{\DrawTangent}[5][]{%
% #1 = draw options
% #2 = name of curve
% #3 = ymin
% #4 = ymax
% #5 = x value at which tangent is to be drawn
\path[name path=Vertical Line Left] (#5-\DeltaX,#3) -- (#5-\DeltaX,#4);
\path[name path=Vertical Line Right] (#5+\DeltaX,#3) -- (#5+\DeltaX,#4);
\path [name intersections={of=Vertical Line Left and #2}];
\coordinate (X0) at (intersection-1);
\path [name intersections={of=Vertical Line Right and #2}];
\coordinate (X1) at (intersection-1);
\draw [shorten <= -1.00cm, shorten >= -1.00cm, #1] (X0) -- (X1);
}%
\begin{document}
\begin{tikzpicture}
% Axes
\draw [->, name path=x] (-1,0) -- (11,0) node [right] {$x$};
\draw [->] (0,-1) -- (0,6) node [above] {$y$};
% Origin
\node at (0,0) [below left] {$0$};
% Points
\coordinate (start) at (1,-0.8);
\coordinate (c1) at (3,3);
\coordinate (c2) at (5.5,1.5);
\coordinate (c3) at (8,4);
\coordinate (end) at (10.5,-0.8);
% show the points
\foreach \n in {start,c1,c2,c3,end} \fill [black] (\n)
circle (2pt) node [below] {};
% join the coordinates
\draw [thick,name path=curve] (start) to[out=70,in=180] (c1) to[out=0,in=180]
(c2) to[out=0,in=180] (c3) to[out=0,in=150] (end);
% add tangets and dashed lines
\foreach \c in {1,2,3} {
%\draw [dashed] let \p1=(c\c) in (c\c) -- (\x1,0) node [below] {$c_\c$};
\draw ($(c\c)-(0.75,0)$) -- ($(c\c)+(0.75,0)$) node [midway,above=4mm]{$$}; %{$f'(c_\c)=0$};
}
% add a and b
\path [name intersections={of={x and curve}, by={a,b}}]
(a) node [below left] {$a$}
(b) node [above right] {$b$};
\DrawTangent[red, thick]{curve}{-1}{4}{1.5}
\DrawTangent[orange, thick]{curve}{-1}{4}{3.5}
\end{tikzpicture}
\end{document}
答案2
PSTricks 解决方案仅用于比较目的。
\documentclass[pstricks]{standalone}
\usepackage{pstricks-add,pst-vehicle,tikzducks}
\def\V{\rput(1,1.5){\psscalebox{-2 2}{\tikz{\duck[alien=red]}}}}
\def\F{2*cos(x)+x/3}
\def\Fp{Derive(1,\F)}
\def\tangent#1#2#3{\psplotTangent[linecolor=blue,showpoints]{#1}{.5}{\F}\uput[#2](*#1 {\F}){\scriptsize$f'#30$}}
\def\normal#1{\psplotTangent[linecolor=magenta,Derive={-1/\Fp},showpoints]{#1}{.2}{\F}}
\psset{plotpoints=200}
\begin{document}
\foreach \i in {-0.8,-0.6,...,10.8}{
\begin{pspicture}(-2.5,-2)(12.5,6)
\psaxes[ticks=none,labels=none,linecolor=gray]{->}(0,0)(-2,-1.5)(11.5,5)[$x$,0][$y$,90]
\psplot[linecolor=red]{-1.5}{11}{\F}
\normal{-1.5}
\tangent{-1}{180}{>}
\tangent{0.167}{-90}{=}
\tangent{1.334}{0}{<}
\tangent{2.974}{-90}{=}
\tangent{4.7115}{0}{>}
\tangent{6.449}{-90}{=}
\tangent{7.852}{0}{<}
\tangent{9.256}{-90}{=}
\tangent{10.5}{0}{>}
\normal{11}
\psVehicle[vehicle=\SelfDefinedVehicle,ownvehicle=\V,showSlope=false,frontwheel=\SpokesWheelB,backwheel=\SpokesWheelA,rB=1.2,rF=0.9,d=3]{.2}{\i}{\F}
\end{pspicture}}
\end{document}
答案3
这只是为了好玩,而不是“窃取” Peter Grill 的勾号,在我看来,他应该得到它。从概念上讲,它基本上与杰克的开创性答案。此答案有两种样式:tangent at
和normal at
,您可以将其传递给曲线,并使用decorations.markings
而不是intersections
。如果您想绘制多条切线和/或法线,则使用会很有利/.list
,如在 MWE 中一样:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
\pgfkeys{tikz/.cd,
tangent length/.store in=\TangentLength,
tangent length=7mm,
normal length/.store in=\NormalLength,
normal length=7mm}
\tikzset{tangent/.style={red,thin},normal/.style={blue,thin},
tangent at/.style={postaction={decorate,decoration={markings,
mark=at position #1 with {\draw[tangent] (-\TangentLength,0) -- (\TangentLength,0);
\fill[tangent] (0,0) circle (2pt);}}}},
normal at/.style={postaction={decorate,decoration={markings,
mark=at position #1 with {\draw[normal] (0,-\NormalLength) -- (0,\NormalLength);
\fill[normal] (0,0) circle (2pt);}}}},
}
\begin{tikzpicture}
% Axes
\draw [-latex] (-1,0) -- (11,0) node [right] {$x$};
\draw [-latex] (0,-1) -- (0,6) node [above] {$y$};
% Origin
\node at (0,0) [below left] {$0$};
% Points
\coordinate (start) at (1,-0.8);
\coordinate (c1) at (3,3);
\coordinate (c2) at (5.5,1.5);
\coordinate (c3) at (8,4);
\coordinate (end) at (10.5,-0.8);
% show the points
% \foreach \n in {start,c1,c2,c3,end} \fill [black] (\n)
% circle (2pt) node [below] {};
% join the coordinates
\draw [thick,tangent at/.list={0.15,0.3,...,0.75},
normal at/.list={0.25,0.4,...,0.7}] (start) to[out=70,in=180] (c1) to[out=0,in=180]
(c2) to[out=0,in=180] (c3) to[out=0,in=150] (end);
\end{tikzpicture}
\end{document}
如果你想知道:是的,可以在曲线经过的某个特定点处绘制切线。一种可能性是使用以下技巧这个答案,即将路径分解为段,并将切线/法线附加到这些段的起点或终点。如果您想要详细说明这一点,请提出一个新问题,这里只是为了好玩。
附录:至于您在评论中提出的请求......(是的,我正在使用杰克的答案,正如我上面提到的)......
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations.markings,intersections}
\begin{document}
\pgfkeys{tikz/.cd,
tangent length/.store in=\TangentLength,
tangent length=3.14cm,
normal length/.store in=\NormalLength,
normal length=7mm}
\newcounter{tangent}
\newcounter{normal}
\tikzset{tangent/.style={red,thin},normal/.style={blue,thin},
tangent at/.style={postaction={decorate,decoration={markings,
mark=at position #1 with {\stepcounter{tangent}
\draw[tangent,name path=tangent-\thetangent] (-\TangentLength,0) -- (\TangentLength,0);
\fill[tangent] (0,0) circle (2pt);}}}},
normal at/.style={postaction={decorate,decoration={markings,
mark=at position #1 with {\stepcounter{normal}
\draw[normal,name path=normal-\thenormal] (0,-\NormalLength) -- (0,\NormalLength);
\fill[normal] (0,0) circle (2pt);}}}},
}
\begin{tikzpicture}
% Axes
\draw [-latex] (-1,0) -- (11,0) node [right] {$x$};
\draw [-latex] (0,-1) -- (0,6) node [above] {$y$};
% Origin
\node at (0,0) [below left] {$0$};
% Points
\coordinate (start) at (1,-0.8);
\coordinate (c1) at (3,3);
\coordinate (c2) at (5.5,1.5);
\coordinate (c3) at (8,4);
\coordinate (end) at (10.5,-0.8);
% show the points
% \foreach \n in {start,c1,c2,c3,end} \fill [black] (\n)
% circle (2pt) node [below] {};
% join the coordinates
\draw [name path=curve,thick,tangent at/.list={0.15,0.3,...,0.75},
normal at/.list={0.25,0.4,...,0.7}] (start) to[out=70,in=180] (c1) to[out=0,in=180]
(c2) to[out=0,in=180] (c3) to[out=0,in=150] (end);
\fill[name intersections={of=curve and tangent-2,total=\t},cyan]
(intersection-\t) circle (2pt);
\end{tikzpicture}
\end{document}
答案4
使用tzplot
包裹:
\documentclass[tikz]{standalone}
\usepackage{tzplot}
\begin{document}
\begin{tikzpicture}
\tzhelplines(-1,-1)(11,6)
\tzaxes*(-1,-1)(11,6){$x$}{$f(x)$}
\tzcoors*(1,-.8)(start)(3,3)(c1)(5.5,1.5)(c2)(8,4)(c3)(10.5,-.8)(end);
\tztos[thick]"curve"(start)[out=70,in=180](c1)[out=0,in=180]
(c2)[out=0,in=180](c3)[out=0,in=150](end);
\tztangent{curve}(c1)[2:4]
\tztangent{curve}(c2)[4.5:6.5]
\tztangent{curve}(c3)[7:9.5]{$f'(x)=0$}[r]
\tzslopeat[red,<->]{curve}{8}{2cm}[90]
\tztangentat[red]{curve}{2}[1:4]{$f'(x)$}[r]
\tzsecantat[blue]{curve}{5.5}{6.3}
\tzsecantat[blue]{curve}{5.5}{6.5}
\tzsecantat[blue]{curve}{5.5}{6.7}
\tzsecantat[blue]{curve}{5.5}{6.9}
\end{tikzpicture}
\end{document}