# How to draw a tangent line to the following curve?

I want to draw the following diagram

Here is my MWE:

\documentclass[border=3mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xtick = \empty,    ytick = \empty,
xlabel = {$x$},
x label style = {at={(1,0)},anchor=west},
ylabel = {$y$},
y label style = {at={(0,1)},rotate=-90,anchor=south},
axis lines=center,
enlargelimits=0.2,
]
\addplot[color=blue,mark=*,label={right:$P$}] (2,4);
\end{axis}
\end{tikzpicture}
\end{document}

Just for fun: pstricks-add has a \psplotTangent command which accepts three arguments: the abscissa of the point of contact, the length of both sides of the tangent segment and the function:

\documentclass[svgnames, x11names, border = 5pt]{standalone}%
\usepackage[utf8]{inputenc}
\usepackage{pst-eucl, auto-pst-pdf}%

\begin{document}

\psset{xunit=2.2cm, yunit = 2cm, arrowinset = 0.12, algebraic, plotstyle = curve, plotpoints = 100}

\begin{pspicture*}(-2,-1.2)(3,3)
\psplot{-2}{2.7}{x^2}
\uput[l](-1,1){$y = x^2$}
\pstGeonode[PosAngle = {0,180}, PointName=](1,1){P}(1.6,2.56){Q}
\uput[r](P){$P(1,1)$}\uput[l](Q){$Q(x, x^2)$}
\pstLineAB[linecolor = LightSteelBlue, nodesep = -5, showpoints]{P}{Q}
\psplotTangent[linecolor = SkyBlue, showpoints]{1}{2.5}{x^2}
\psaxes[linewidth = 0.6pt, labels = none, ticks = none, arrows = ->](0,0)(-2,-1.2)(3,3)[$x$, -110][$y$,210]
\uput[dl](0,0){$0$}
\end{pspicture*}

\end{document}

Of course, it's better to have a functional expression for the tangent line, which in this example is a simple exercise. However, Asymptote offers a "calculus-free" way of drawing the tangent lines. There is a built-in function dir(path, time) exactly for this purpose.

Let's pretend, that we don't know how to obtain the tangent line equation for the function.

Given the function curve guide gf and x=1, we can use built-in function times

t=times(gf,x)[0];

to get a value of the time parameter t, which corresponds to the intersection of the function curve and a vertical line at x=1. This t value allows to: first, get a missing 'y' coordinate of the tangent point P

P=point(gf,t);

And second, the direction of the tangent line at this point as dir(gf,t).

Function drawline (part of the basic Asymptote module math.asy), allows to draw the visible portion of the (infinite) line going through two points:

drawline(P,P+dir(gf,t),tanLinePen);

Another useful function for this drawing is relpoint, which returns the point on curve at the relative fraction of its arclength.

This is a complete MWE:

// tan.asy
//
// run
// asy tan.asy
//
// to get tan.pdf
//
settings.tex="pdflatex";
import graph; import math;
size(6cm);
import fontsize;defaultpen(fontsize(10pt));
texpreamble("\usepackage{lmodern}"
+"\usepackage{amsmath}"
+"\usepackage{amsfonts}"
+"\usepackage{amssymb}"
);
pen funcLinePen=darkblue+0.9bp;
pen tanLinePen=orange+0.9bp;
pen grayPen=gray(0.3)+0.8bp;
pen dashPen=gray(0.3)+0.8bp+linetype(new real[]{5,5})+linecap(0);
real xmin=-2,xmax=-xmin;
real ymin=0,ymax=4;
real dxmin=0.2;
real dxmax=dxmin;
real dymin=dxmin;
real dymax=dxmax;

xaxis("$x$",xmin-dxmin,xmax+dxmax,RightTicks(Step=1,step=0.5),arr,above=true);
yaxis("$y$",ymin-dymin,ymax+dymax,LeftTicks (Step=1,step=0.5,OmitTick(0)),arr,above=true);

real f(real x){return x^2;}

guide gf=graph(f,xmin,xmax,operator..);

real x=1, t=times(gf,x)[0];
pair P=point(gf,t), Q=relpoint(gf,6/7);

draw(gf,funcLinePen);
draw((P.x,0)--P--(0,P.y),dashPen);
drawline(P,P+dir(gf,t),tanLinePen);
dot((P.x,0)^^P^^(0,P.y)^^Q,UnFill);
label("$y=x^2$",relpoint(gf,1/4),UnFill);
label("$P(1,"+string(round(P.y))+")$",P,plain.SE);
label("$Q(x,x^2)$",Q,plain.W);
label("$T$",Q,3*plain.E);

I'm surprised that the original question is tagged but one answer (the accepted one) uses and the other uses ! So I decide to post a TikZ answer, but this is mainly a mathematics answer.

\documentclass[tikz]{standalone}
\usetikzlibrary{through}
\begin{document}
\begin{tikzpicture}[declare function={f(\x)=\x*\x;}]
\draw[-latex] (-2.5,0)--(2.5,0) node[below] {$x$};
\draw[-latex] (0,-2)--(0,2.5) node[left] {$y$};
\draw plot[smooth,samples=100,domain=-1.5:1.5] (\x,{f(\x)});
\path (0,0) node[below left] {$0$};
\fill (1,{f(1)}) coordinate (p) circle (1pt);
\coordinate (x) at (0,{1/(4*f(1))});
\node[circle through={(p)}] (cir) at (x) {};
\draw[shorten >=-1.5cm,shorten <=-1cm] (cir.south)--(p);
\end{tikzpicture}
\end{document}

This code works for any P on the parabola once the equation of the parabola is still in the form of ax2. For general case ax2 + bx + c, please edit the coordinate of (x)!

\documentclass[tikz]{standalone}
\usetikzlibrary{through}
\begin{document}
\begin{tikzpicture}[declare function={f(\x)=2*\x*\x;}]
\draw[-latex] (-2.5,0)--(2.5,0) node[below] {$x$};
\draw[-latex] (0,-2)--(0,2.5) node[left] {$y$};
\draw plot[smooth,samples=100,domain=-1.5:1.5] (\x,{f(\x)});
\path (0,0) node[below left] {$0$};
\fill (-.5,{f(-.5)}) coordinate (p) circle (1pt);
\coordinate (x) at (0,{1/(4*f(1))});
\node[circle through={(p)}] (cir) at (x) {};
\draw[shorten >=-1.5cm,shorten <=-1cm] (cir.south)--(p);
\end{tikzpicture}
\end{document}

### Proof of correctness

I find the focus of the parabola, after that I can find another point on the tangent line.

\documentclass[tikz]{standalone}
\usetikzlibrary{through}
\begin{document}
\begin{tikzpicture}[declare function={f(\x)=\x*\x;}]
\draw[-latex] (-2.5,0)--(2.5,0) node[below] {$x$};
\draw[-latex] (0,-2)--(0,2.5) node[left] {$y$};
\draw plot[smooth,samples=100,domain=-1.5:1.5] (\x,{f(\x)});
\path (0,0) node[below left] {$0$};
\fill (1.2,{f(1.2)}) coordinate (p) circle (1pt);
\coordinate (x) at (0,{1/(4*f(1))});
\fill (x) circle (1pt);
\node[draw,very thin,dashed,circle through={(p)}] (cir) at (x) {};
\fill (cir.south) circle (1pt);
\draw[shorten >=-1.5cm,shorten <=-1cm] (cir.south)--(p);
\end{tikzpicture}
\end{document}