\section{Zeros and extrema}

Lua finds all the zeros of a function on an interval: the interval is cut into \tkzname{samples} steps, each change of sign is refined by bisection, and the zeros without change of sign (double root, the curve touches the axis) are found too. A change of sign without a zero, on both sides of an asymptote, is not taken for a zero. The extrema are the zeros of the derivative where it changes sign.

\subsection{Defining the zeros: \tkzcname{tkzFnZeros}}

\begin{NewMacroBox}{tkzFnZeros}{\oarg{options}\parg{name of the function}}
\emph{Defines the points \texttt{z1}, \texttt{z2}, \ldots\ of the curve, in increasing order of $x$. Their number is in \tkzcname{tkzFnNbZeros}. In a \texttt{tikzpicture} environment, the points are also \TIKZ\ nodes; outside, they are only stored, for \tkzcname{tkzFnPrintCoord}.}

\begin{tabular}{lll}
\toprule
options             & default & definition                         \\
\midrule
\TOline{at}{zeros}{\texttt{zeros} or \texttt{extrema}}
\TOline{domain}{xmin:xmax}{interval of the search}
\TOline{samples}{400}{number of steps of the scan}
\TOline{name}{z}{prefix of the names of the points}
\TOline{draw}{false}{the points are drawn}
\TOline{size}{1.5pt}{radius of the points}
\bottomrule
\end{tabular}

\emph{Other options are \TIKZ\ options, for drawing the points.}
\end{NewMacroBox}

\begin{tkzexample}[latex=8cm]
\begin{tikzpicture}
  \tkzFnInit[xmin=-3,xmax=3,ymin=-2,ymax=5]
  \tkzFnGrid
  \tkzFnAxes
  \tkzFnDraw[thick,name=g]{(x-1)^2*(x+2)}
  \tkzFnZeros[draw,red](g)
  \tkzFnLabelPoints[above left](z1)
  \tkzFnZeros[at=extrema,name=e,draw,blue](g)
  \tkzFnLabelPoints[above](e1,e2)
\end{tikzpicture}
\end{tkzexample}

$g(x)=(x-1)^2(x+2)$ has \tkzFnZeros(g)\tkzFnNbZeros{} zeros: $z_1=\tkzFnPrintCoord(z1)$ and the double root $z_2=\tkzFnPrintCoord(z2)$, which is also the local minimum $e_2$. The local maximum is $e_1=\tkzFnPrintCoord(e1)$.

The text above is written with |\tkzFnZeros(g)\tkzFnNbZeros{} zeros: $z_1=\tkzFnPrintCoord(z1)$|, after the picture.

\subsection{Tangent lines at the zeros and at the extrema}

With the option \tkzname{at}, \tkzcname{tkzFnTangent} needs no abscissa: it draws the tangent line at each zero (\texttt{at=zeros}) or the horizontal tangent line at each extremum (\texttt{at=extrema}). The options \tkzname{domain} and \tkzname{samples} have the same meaning as for \tkzcname{tkzFnZeros}; the other options are those of \tkzcname{tkzFnTangent}.

$f(x)=\mathrm{e}^{-x/5}\bigl(\sin(1.5x)+0.6\cos(2.5x)\bigr)$: none of these points can be computed by hand. The two extrema close to $x\approx1.7$ are well separated.

\begin{tkzexample}[vbox]
\begin{tikzpicture}[scale=1.4]
  \tkzFnInit[xmin=0,xmax=7,ymin=-0.75,ymax=1.25,ystep=0.25]
  \tkzFnAxes
  \tkzFnDraw[name=f,samples=400,thick]
     {exp(-x/5)*(sin(1.5*x)+0.6*cos(2.5*x))}
  \tkzFnTangent[at=zeros,draw,red,kl=.45,kr=.45](f)
  \tkzFnTangent[at=extrema,draw,teal,dashed,kl=.5,kr=.5](f)
\end{tikzpicture}
\end{tkzexample}

\subsection{Discontinuous function}

$\tan$ changes sign on both sides of each asymptote without vanishing: these changes of sign are discarded.

\begin{tkzexample}[latex=8cm]
\begin{tikzpicture}[scale=.8]
  \tkzFnInit[xmin=-4,xmax=4,ymin=-3,ymax=3]
  \tkzFnGrid
  \tkzFnAxes
  \tkzFnDraw[thick,name=t]{tan(x)}
  \tkzFnTangent[at=zeros,draw,red,
                kl=.6,kr=.6](t)
\end{tikzpicture}
\end{tkzexample}

\subsection{Limits}

\begin{itemize}
\item Two zeros closer than one step of the scan may be missed: increase \tkzname{samples} or reduce \tkzname{domain}.
\item The bounds of the interval are never extrema: only the points where the derivative vanishes and changes sign are kept. For $x\mapsto x^3$, there is no extremum at $0$.
\end{itemize}

\endinput
