7

I am trying to replicate a diagram and have a minimal example. I don't know how to add additional tangents between the specified points and preserve smoothness.

\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}

Outputs:

enter image description here My Goal is create the following:

enter image description here

Feel free to make any changes that make the minimal example approach the goal diagram

7

You can use a bit of calculus:

enter image description here

The \DrawTangent macro creates two vertical lines around the given x values and computes the intersection of those and the curve. Then, the tangent can be approximated by the secant between those two intersection points.

Code:

\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}
  • I really like this answer plus the macro. I will allow other some time for others answer as well but this is what I was looking for! – MathScholar Nov 24 '18 at 17:31
  • Hi Peter if I were to name the curve by \draw[domain=.5:3.25,smooth,variable=\x,black,<->,thick] plot ({\x},{.5*(\x-1.5)*(\x-1.5)+1}); how would the syntax change> – MathScholar Nov 27 '18 at 19:52
  • @MathScholar: You need to use name path=xxxx to name the curve (xxxx in this example), and pass that to \DrawTangent[<draw options>]{xxxx}{<ymin>}{<ymax>}{x}. – Peter Grill Nov 27 '18 at 21:09
  • Yes I did that and got a horizontal line. I ask as a question as well so you can see the result and program. I thought this would be more easy to do! Here: tex.stackexchange.com/questions/462070/… – MathScholar Nov 27 '18 at 21:13
  • Sorry Peter I made a mistake! I got it! I deleted he question – MathScholar Nov 27 '18 at 21:19
9

A PSTricks solution just for comparison purpose.

enter image description here

\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}
  • 2
    Many compliments for your answer. +1. – Sebastiano Nov 24 '18 at 16:30
  • This looks good but I am searching for an answer using using TiKz. Thanks AS! – MathScholar Nov 24 '18 at 16:34
  • Well, this is certainly nice but of course here you have an analytic expression for the curve. If you have this analytic expression, it is well known how to find the local extremal and tangents and all that, see e.g. here. I thought the challenge here was to do that to curves for which the analytic expression is not known. (This is not to criticize you, just to let you know that there exists quite some literature on this topic on this site.) – user121799 Nov 24 '18 at 19:53
  • @marmot: Analytic expression is no longer as complicated as the previous one. – Artificial Stupidity Nov 25 '18 at 11:04
  • I presume if the "roller coaster" figure here were done instead with tikz, then the creature riding in the car would be a duck?? – murray Nov 25 '18 at 15:43
5

This is just for fun and not to "steal" the check mark from Peter Grill, who IMHO should get it. Conceptually it is basically the same as Jake's pioneering answer. This answer comes with two styles: tangent at and normal at, which you can pass to the curve, and utilizes decorations.markings rather than intersections. If you want to draw several tangents and/or normals, it is advantageous to use /.list, as in the 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}

enter image description here

Just if you are wondering: yes, it is possible to draw the tangent at some specific point the curve runs through. One possibility is to use the tricks of this answer, i.e. decompose the path into segments and attach the tangents/normals to the starting or end points of those. If you want to have this spelled out, please ask a new question, this here is really just for fun.

ADDENDUM: As for your request in the comments... (and yes, I am using Jake's answer, as I mention above)....

\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}

enter image description here

  • this looks fantastic as well and uses methods from tex.stackexchange.com/questions/25928/… I believe. How can I extend the second tangent starting from the left and use the intersection, calc package to show where it intersects the curve further towards the right? – MathScholar Nov 24 '18 at 20:50
  • @MathScholar I added something. (I guess on the long run one may have to look at the internals of the intersections and fillbetween libraries, which internally compute all sorts of of useful quantities like \pgfintersectiongetsolutiontimes, which however did not make it to the manual.) – user121799 Nov 24 '18 at 21:04
  • per your request I will give the check to Peter. Both posts are extremely helpful. Thanks Marmot! – MathScholar Nov 24 '18 at 22:17
  • @MathScholar Thanks! (I generally do not like if a question that has already an answer that is to the point gets a second answer, which is not to the point or just merely a repetition, like e.g. here. This brings unnecessary tension between the users IMHO, and the last thing I want to do is to step others on the feet.) – user121799 Nov 24 '18 at 22:32
  • @Hi Marmot, if I continue to extend the tangent to the right and fix the left side, there will be another intersection of the curve and tangent. Why does the intersection not get detected by the program? – MathScholar Nov 24 '18 at 23:46
1

Here is a macro inspired by @PeterGrill's answer in which I tried to automate the calculations. This macro is called \secante because it allows you to draw both a tangent and a normal to the curve.

To draw the two vertical lines, I drew these vertical lines using the current bounding box, which avoids manual calculations.

bounding-box

This allows me to add parameters such as the length and its angle with the tangent.

  % #1 = draw options
  % #2 = name of curve
  % #3 = x value at which tangent is to be drawn
  % #4 = length of the tangent (approximation)
  % #5 = angle with tangent,0 by default, 90 for normal

rotate

tangente-normale

The macro first determines the points where the verticals intersect with the curve. They are shown below in blue. The two points adjacent to the tangent contact point are called "gauche of curve" and "droite of curve".

definitions-points

This being done, with the calc library, the contact point is projected orthogonally on the segment (gauche of curve)--(droite of curve)

Then, we make a translation, of these points to find the two end points of this tangent.

It is then enlarged.

exemple

Here is an example produced by the code below:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc,intersections,arrows.meta}
\newcommand{\secante}[5][]{
\tikzset{every node/.style={font=\scriptsize}}
  % #1 = draw options
  % #2 = name of curve
  % #3 = x value at which tangent is to be drawn
  % #4 = length of the tangent (approximation)
  % #5 = angle avec la tangente,0 par défaut, 90 pour la normale
\path[name path=verticale] (current bounding box.south west -|#3,0)--(#3,0 |-current bounding box.north east);
\path[name path=gauche] (current bounding box.south west -|#3-.01,0)--(#3-.01,0 |-current bounding box.north east);
\path[name path=droite] (current bounding box.south west -|#3+.01,0)--(#3+.01,0 |-current bounding box.north east);
% point de contact de la tangente
\path [name intersections={of= verticale and #2,by=point of tangency}];
% points d'approximation de la sécante (parallèle)
\path [name intersections={of= gauche and #2,by=gauche of #2}];
\path [name intersections={of= droite and #2,by=droite of #2}];
% projection du point de contact sur la parallèle
\coordinate(projection) at ($(gauche of #2)!(point of tangency)!(droite of #2)$);
% points de la sécante
\coordinate(gauche of secant line) at ($(gauche of #2)+(point of tangency)-(projection)$);
\coordinate(droite of secant line) at ($(droite of #2)+(point of tangency)-(projection)$);
% secant ou normale
\draw[#1]($(gauche of secant line)!#4/2!#5:(droite of secant line)$)--($(droite of secant line)!#4/2!#5:(gauche of secant line)$);
}

\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 [semithick,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);

\secante[red,thick]{curve}{3}{2cm}{0}
\secante[red,thick]{curve}{3}{2cm}{0} % tangent to the curve
\secante[red,thick]{curve}{5}{2cm}{0}
\secante[red,thick]{curve}{7}{2cm}{0}
\secante[red,thick]{curve}{9}{2cm}{0}
\secante[blue,thick]{curve}{8}{1cm}{90}
\secante[blue,thick]{curve}{10}{1cm}{90} % normal to the curve
\end{tikzpicture}

\end{document}

Translated with www.DeepL.com/Translator

  • Thanks Andre' I understand some of the French words in the program and like the animation as well. – MathScholar Nov 29 '18 at 0:25
  • @MathScholar Do I have to translate the code comments into English? – AndréC Nov 29 '18 at 5:31
  • No I understand everything. I studied French in Paris for several months! – MathScholar Nov 29 '18 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.