# Tikz and Secant Line diagram with axis

I'm trying to modify the following diagram to also show the distances on the x and y axes. Here is a code from another post

\documentclass[10pt]{article}

\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[declare function={func(\y) = 0.1*(\y-5)*(\y-5)+1;}]
\draw[domain=2:15,smooth,variable=\x,thick] plot ({\x},{func(\x)});
\draw[fill] (6.4,{func(6.4)})node[below]{P}circle (2pt)coordinate(p);
\foreach[count=\i] \x in {8.0,9.6,...,14.4}{
\draw[fill] (\x,{0.1*(\x-5)*(\x-5)+1})node[below]{Q$_\i$} circle (2pt)coordinate(Q\i);
\draw[thick,blue!80,dashed,shorten >=-2cm,shorten <=-2cm] (p) -- (Q\i)node[right=0.7cm](m\i){Secante $s_\i$};
}
\draw[thick,red!70,shorten >=-9cm,shorten <=-4cm] (p) -- (6.401,{func(6.401)});
\draw[-latex,line width=4mm,red!20] (m4.south east) to[out=-100, in=25] (m2.south east)node[below,anchor=north west,red]{tangent $t$};
\end{tikzpicture}

\end{document}


This will output

I am trying to go here with the picture:

This is a bit beyond my programming skills I think? Please all suggestions welcome.

• Can the function just be half of a parabola, do you need a specific function? Do the points need to lie on a specific point? Commented Apr 26, 2023 at 23:06
• The function can just be half of a parabola. Both would be interesting, so once with specific points as well as in general. Commented Apr 26, 2023 at 23:12

The parabola is drawn with the path operation parabola which is more efficient than a plot, its function is known and so is its derivative. They are declared as well as the x value for P₀ since it comes up a few times.

The markx key adds a line to the x axis (that's edge[dashed]) and a small tick (the edge without options) as well as the actual label. Both the position on the x axis as well as the node for the label are named so that we can reference them later. (Since it uses an edge the lines borrow their appearence from the path the node is one … if there were any.)

We place P₀ separately than Pi since the xmarks are also different. Otherwise or with some conditionals they could be done in one loop.

Inside a scope the current bounding box of the diagram is used to define a clip box so that the tangents and secants don't get drawn outside of it. (This could also be solved via math or some PGF-level macros but this makes it very easy.)

The calcs library's distance modifier is used to draw lines between P₀ and the other points as well as the tangent by using distances that are larger than the diagram. (A reasonable very safe distance would be length of the diagonal of the page but here 10cm and 15cm are enough.)

Afterwards the red arrow is drawn using the named mark nodes, the text “Sekanten” and “Tangente” are placed using the undocumented intersection of coordinate system. There's also the intersections library but for straight lines, the intersection of syntax should be enough.

The labels for the point P₁, P₂ and P₃ are placed by finding their projection on the upper most secant, though this doesn't look very good anymore in my example.

Maybe a transparent node should be placed instead, at least for P₃ like

\foreach \i in {1, ..., 3}
\node also [label={[inner sep=.15em, circle, fill=white,
fill opacity=.5, text opacity=1]above left:$P_{\i}$}] (P\i);


## Code

\documentclass[tikz, convert]{standalone}
\usetikzlibrary{arrows.meta, calc}
\begin{document}
\begin{tikzpicture}[
> = Latex,
declare function={
f(\x)  = 0.09*\x*\x + 1;
ft(\x) = 0.18*\x;
p0x    = 1.5;
},
dot/.style = {circle, fill, draw, inner sep = +0pt, minimum size = +3pt},
xmark/.style = {
append after command = {
\bgroup
\pgfextra{\let\tln\tikzlastnode}
coordinate (\tln-x) at (\tln|-0,0) edge[dashed] (\tln)
(\tln-x) edge node[below](\tln-mark){$#1\vphantom{h}$} +(down:3pt)
\egroup}},
]
\draw[thick] (0,1) node[above right]{$G(f)$}
parabola (10,10);% ⇒ f(x) = (10−1)/10² x² + 1
\draw[->] (left:1) -- (right:10) node[below left] {$x$};
\draw[->] (down:1) -- (   up:10) node[below left] {$y$};

\node[dot, xmark=x_0, label=$P_0$] (P0) at (p0x,{f(p0x)}) {};
\foreach[count=\i] \x in {8, 5, 3}
\node[dot, xmark=x_0+h_{\i}] (P\i) at (\x,{f(\x)}) {};

\begin{scope}
\clip (current bounding box.south west) rectangle (current bounding box.north east);
\draw[red] ($(P0)!-10cm!($(P0)+(1,{ft(p0x)})$)$) coordinate (P0-left)
-- ($(P0)! 10cm!($(P0)+(1,{ft(p0x)})$)$) coordinate (P0-right);
\foreach \i in {1, ..., 3}
\draw ($(P0)!-15cm!(P\i)$) coordinate (P\i-left)
-- ($(P0)! 15cm!(P\i)$) coordinate (P\i-right);
\end{scope}

\draw[red, <-] (P0-mark.south) -- (P1-mark.south);
\foreach \i/\c/\Text in {0/red/Tangente, 2/black/Sekanten}
\node[\c, below right] at (intersection of P1--P1-x and P0--P\i-right) {\Text};

\foreach \i in {1, ..., 3}
\node[above left] at ($(P0)!(P\i)!(P1)$) {$P_{\i}$};
\end{tikzpicture}
\end{document}


## Output

Using tzplot:

\documentclass{standalone}
\usepackage{tzplot}

\begin{document}

\begin{tikzpicture}
%\tzhelplines[thick](10,10)
\settzdotsize{3pt}
\tzaxes*(-1,-1)(10,10){$x$}[b]{$y$}[l]
%% function G(f)
\def\Gf{0.08*(\x)^2+1}
\tzfn[very thick]\Gf[10:0]{$G(f)$}[ar]
%% x_0 point
\tzvXpointat*{Gf}{1.5}(P0){$P_0$}[a]
\tzprojx[dashed](P0){$x_0$}
%% secant lines
\foreach [count=\i] \xx in {8.0,5,3}{%
\tzvXpointat*{Gf}{\xx}(P\i){$P_\i$}[[label distance=-3pt]135]
\tzsecant[blue]{Gf}(P0)(P\i)[-1:10]
\tzprojx[dashed](P\i){$x_0+h_\i$}
}
%% tangent
\tztangent[thick,red]{Gf}(P0)[-1:10]
%% labels
\tznode(8,2.1){Tangente}[ar,red]
\tznode(8,4.1){Sekanten}[ar]
\tzline[->,red](8,-.5)(1.5,-.5)
\end{tikzpicture}

\end{document}