\section{Riemann sums}

\begin{NewMacroBox}{tkzFnRiemann}{\oarg{options}\parg{name of the function}}
\emph{Draws the rectangles of a Riemann sum. The four macros of \tkzname{tkz-fct} are merged into one; the method is chosen with an option.}

\begin{tabular}{lll}
\toprule
options             & default & definition                         \\
\midrule
\TOline{interval}{xmin:xmax}{interval where the method is applied}
\TOline{number}{10}{number of subintervals}
\TOline{method}{left}{height of the rectangles: see below}
\TOline{clip}{false}{the rectangles are clipped to the window}
\bottomrule
\end{tabular}

\begin{tabular}{ll}
\toprule
method & height of the rectangle on $[x_i, x_{i+1}]$ \\
\midrule
\texttt{left}  & $f(x_i)$ \\
\texttt{right} & $f(x_{i+1})$ \\
\texttt{mid}   & $f\left(\frac{x_i+x_{i+1}}{2}\right)$ \\
\texttt{inf}   & $\min\big(f(x_i),f(x_{i+1})\big)$ \\
\texttt{sup}   & $\max\big(f(x_i),f(x_{i+1})\big)$ \\
\bottomrule
\end{tabular}

\emph{Other options are \TIKZ\ options; the style is \tkzname{tkzfn riemann}. The value of the sum is given by \texttt{riemann(f,a,b,n,"left")}.}
\end{NewMacroBox}

\subsection{A Riemann sum}

\begin{tkzexample}[vbox]
\begin{tikzpicture}[scale=3.5]
\tkzFnInit[xmin=0,xmax=3,ymin=0,ymax=1.75]
\tkzFnAxes
\tkzFnGrid(0,0)(3,1.75)
\tkzFnDraw[name=f,color=red,domain=1/3:3]{0.125*(3*x-1)+0.375*(3*x-1)/(x*x)}
\tkzFnRiemann[fill=green!40,opacity=.2,color=green,
              line width=1pt,interval=1/2:e,number=10](f)
\end{tikzpicture}
\end{tkzexample}

\newpage
\subsection{Methods \tkzname{inf} and \tkzname{sup}}

\begin{tkzexample}[vbox]
\begin{tikzpicture}[scale=1.75]
  \tkzFnInit[xmin=-3,xmax=6,ymin=-2,ymax=14,ystep=2]
  \tkzFnAxes[ticks=false]
  \tkzFnDraw[name=f,line width=2pt,color=red,domain=-3:6]{(-x-2)*(x-5)}
  \tkzFnRiemann[method=sup,fill=blue!40,opacity=.5,interval=-1:5](f)
  \tkzFnRiemann[method=inf,fill=green!40,opacity=.5,interval=-1:5](f)
\end{tikzpicture}
\end{tkzexample}

\newpage
\subsection{Method \tkzname{mid}}

\begin{tkzexample}[vbox]
\begin{tikzpicture}[scale=1.75]
  \tkzFnInit[xmin=-3,xmax=6,ymin=-2,ymax=14,ystep=2]
  \tkzFnAxes[ticks=false]
  \tkzFnDraw[name=f,line width=2pt,color=red,domain=-3:6]{(-x-2)*(x-5)}
  \tkzFnRiemann[method=mid,fill=blue!40,opacity=.5,interval=-1:5](f)
\end{tikzpicture}
\end{tkzexample}

\subsection{Values of the sums}

For $f(x)=(-x-2)(x-5)$ on $[-1, 5]$ with 10 rectangles:

\begin{tkzexample}[latex=6cm]
\tkzFnDef{f}{(-x-2)*(x-5)}
\begin{tabular}{ll}
inf      & \tkzFnPrint{riemann(f,-1,5,10,"inf")}\\
mid      & \tkzFnPrint{riemann(f,-1,5,10,"mid")}\\
sup      & \tkzFnPrint{riemann(f,-1,5,10,"sup")}\\
integral & \tkzFnPrint{integral(f,-1,5)}
\end{tabular}
\end{tkzexample}

\endinput
