5

I would like to draw a tangent line on (x0) which is an intersection between vertical line and the red curve, this is my code

\documentclass[tikz,preview,border=1mm]{standalone}
\usetikzlibrary{calc,intersections,arrows}
\def\w{0.7}
\begin{document}
\begin{tikzpicture}[>=latex]
\draw[->,line width=0.5pt](-0.3,0) -- (5.2,0);
\draw[->,line width=0.5pt](0,-0.3) -- (0,3.5);

\draw[name path=curve,red] plot[samples=100,smooth,domain=0:3,variable=\t] ({1+cos(deg(\w* \t)) * 5},{5+- sin(deg(\w\t)) \w* 5});

\drawname path=vline--+(90:3);

\path[name intersections={of=curve and vline, by=x0}];
\fill (x0) circle(1pt); \end{tikzpicture} \end{document}

  • Maybe https://tex.stackexchange.com/questions/37866/how-to-draw-tangent-vectors-and-component-vectors-on-a-curve can help? – Rmano Jan 30 '24 at 15:09
  • Thanks, but this is not the case, I tried to adopt it with my problem and still nor working! – Hariz Khaled Jan 30 '24 at 15:19
  • This would be much easier with a linear equation where you at least can set x or y to one specific value. – Jasper Habicht Jan 30 '24 at 17:18
  • I treid to adapt https://tex.stackexchange.com/a/477208/38080 but I have a "Dimension too large..." error – Rmano Jan 30 '24 at 17:19

2 Answers2

2

If you can get away with the "smooth" part (which creates numerical instabilities), this (from https://tex.stackexchange.com/a/477208/38080) works:

\documentclass[tikz,preview,border=1mm]{standalone}
\usepackage{pgfplots}\pgfplotsset{compat=newest}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{calc,intersections,arrows}
\usetikzlibrary{decorations.markings}
\tikzset{tangent at/.style={% cf. https://tex.stackexchange.com/questions/25928/how-to-draw-tangent-line-of-an-arbitrary-point-on-a-path-in-tikz/25940#25940
        decoration={ markings,
            mark =at position #1 with {\draw[purple,-latex](0,0) -- (-1,0);},
        }, decorate
    }
}
\def\w{0.7}

\begin{document} \begin{tikzpicture}[>=latex] \draw->,line width=0.5pt -- (5.2,0); \draw->,line width=0.5pt -- (0,3.5);

\draw[name path=curve,red] plot[samples=100,domain=0:3,variable=\t] ({1+cos(deg(\w* \t)) * 5},{5+- sin(deg(\w\t)) \w* 5}); \drawname path=vline--+(90:3); \path[name intersections={of=curve and vline, by=x0}]; \fill (x0) circle(1pt);

\path [draw,blue, name path=section, intersection segments={ of=curve and vline, sequence = {L2}, }, postaction={tangent at=0} ];

\end{tikzpicture} \end{document}

enter image description here

You can remove the draw, blue that is used just to visual see the effect of the sequence key... https://tikz.dev/pgfplots/libs-fillbetween#sec-46.6

Rmano
  • 40,848
  • 3
  • 64
  • 125
2

A more or less straight-forward idea would be to calculate two coordinates that are very close to the relevant coordinate on the plot trough which you want to draw a tangent and then draw a line through these two coordinates.

Since the plot in your example does not use a linear function, I added a reverse function to easier get the right values.

\documentclass[tikz, border=10pt]{standalone}
\usetikzlibrary{intersections, calc}

\begin{document} \begin{tikzpicture}[ >=latex, declare function={ w = 0.7; curvex(\t) = 1+cos(deg(w\t))5; curvey(\t) = 5-sin(deg(w\t))w*5; reversex(\x) = rad(acos((\x-1)/5))/w; }, ]

\draw[->, line width=0.5pt] (-0.3,0) -- (5.2,0); \draw[->, line width=0.5pt] (0,-0.3) -- (0,3.5);

\draw[name path=curve, red] plot[samples=100, smooth, domain=0:3, variable=\t] ({curvex(\t)},{curvey(\t)});

\draw[name path=vline] (3,0) -- +(90:3);

\path[name intersections={of=curve and vline, by=x0}];
\fill (x0) circle[radius=1pt];

\pgfmathsetmacro{\i}{reversex(3)} \coordinate (x1) at ({curvex(\i-0.01)},{curvey(\i-0.01)}); \coordinate (x2) at ({curvex(\i+0.01)},{curvey(\i+0.01)});

\draw[green] ($(x1)!-2cm!(x2)$) -- ($(x1)!2cm!(x2)$);

\end{tikzpicture} \end{document}

enter image description here