banner



How To Draw A Tangent To A Curve

By adaption Jake's code given here to a similar problem, you can add a tangent line at any x coordinate "automatically". This also should work for every \addplot command you state, also the \addplot coordinates command given in the question.

          % used PGFPlots v1.13 and TikZ v3.0.1a \documentclass[border=2mm]{standalone} \usepackage{xcolor}     \colorlet{Curve}{red!75!black}     \colorlet{Tangent}{blue!75!black} \usepackage{pgfplots}     \pgfplotsset{compat=1.10}     \usetikzlibrary{         calc,         intersections,         math,     }     \makeatletter         \def\parsenode[#1]#2\pgf@nil{%             \tikzset{label node/.style={#1}}             \def\nodetext{#2}         }         \tikzset{             % define style for the points             Point/.style={                 shape=circle,                 inner sep=0pt,                 minimum size=3pt,             },             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 [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=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};                     % ---------------------------------------------------------                     % draw the tangent line from a bit right of the point on                     % the curve to the intersection with the ordinate                     % and draw the corresponding points                     \draw [Tangent] let                         \p1=($ (left intersection-1) - (right intersection-1) $),                         \p2=($ (left intersection-1)!sign(#1)*5mm!(right intersection-1) $),                         \p3=($ ({axis cs:0,0}) - (\p2) $),                         \n1={\x3/\x1}                     in                         (\p2) -- +($ {\n1}*(\x1,\y1) $) %                            node [Point,fill=Tangent] (origin intersection) {} %                            node [Point,fill=Curve] at (left intersection-1) {}                     ; %                    % ---------- %                    % draw the horizontal line at the curve intersection point %                    % plus the label above/below the line %                    \tikzmath{ %                        coordinate \c1; %                        \c1=(left intersection-1) - (right intersection-1); %                        \slope=\cy1/\cx1*sign(#1); %                    } %                    \pgfmathsetmacro{\AboveBelow}{ \slope>0 ? "above" : "below" } %                    \draw [dotted] %                        ([xshift=sign(#1)*2.5mm] left intersection-1) -- %                        (left intersection-1) -- %                            node [\AboveBelow,node font=\scriptsize] {$f(x)$} %                        (left intersection-1 -| origin intersection) -- %                        +($ sign(#1)*(-2.5mm,0) $) %                            coordinate [pos=0.5] (a) %                    ; %                    % draw the horizontal line at the ordinate intersection point %                    \draw [dotted] (origin intersection) %                        +($ sign(#1)*(-2.5mm,0) $) -- %                        (origin intersection); %                    % draw vertical line left/right of the ordinate %                    \pgfmathsetmacro{\LeftRight}{ #1<0 ? "right" : "left" } %                    \draw [stealth-stealth] (origin intersection) %                        +($ sign(#1)*(-1.25mm,0) $) -- (a) %                            node [midway,\LeftRight,node font=\scriptsize] {$p$} %                    ; %                    % ---------------------------------------------------------                         \endgroup                 },             },         }     \makeatother \begin{document}     \begin{tikzpicture}         \begin{axis}[             axis lines=middle,             enlargelimits=0.1,             xmin=-0.5,             xmax=3.25,             ymin=0,             ymax=5.5,             xtick=\empty,             ytick=\empty,             xlabel=$x$,             ylabel=$y$,             domain=\pgfkeysvalueof{/pgfplots/xmin}:(\pgfkeysvalueof{/pgfplots/xmax},             samples=50,             tangent/.style={                 add node at x={#1}{},             },         ]             \addplot [thick,draw=Curve,                 tangent=-0.5,                 tangent=0.8,                 tangent=2.3,             ] {0.5*x^2 - 2*x + 4};         \end{axis}     \end{tikzpicture} \end{document}                  

The above code results in

image showing the result of above code

and when you uncomment the commented lines you will get

image showing the result of above code when commented lines are uncommented

How To Draw A Tangent To A Curve

Source: https://tex.stackexchange.com/questions/290241/how-to-draw-a-tangent-line-to-an-arbitrary-addplot

Posted by: jacobsfooster.blogspot.com

0 Response to "How To Draw A Tangent To A Curve"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel