Timing and integration with TikZ PGFlots

How do I set intervals or integration with TikZ PGFlots? Something similar to the figure below, but I want to define the function, the intervals of integration and the partitions. \documentclass{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[upright]{fourier}
\usepackage[usenames,dvipsnames,svgnames]{xcolor}
\usepackage{amsmath,tkz-fct}
\usepackage[active,pdftex,tightpage]{preview}
\PreviewEnvironment{tikzpicture}
\setlength\PreviewBorder{5pt}

\begin{document}

\begin{tikzpicture}[scale=1]
\tkzInit[xmin=-3,xmax=6,ymin=-2,ymax=14,ystep=2]
\tkzDrawX \tkzDrawY
\tkzFct[line width=2pt,color = red, domain =-3:6]{(-\x-2)*(\x-5)}
\tkzDrawRiemannSumSup[fill=blue!40,opacity=.5,interval=-1:5,number=10]
\tkzDrawRiemannSumInf[fill=green!40,opacity=.5,interval=-1:5,number=10]
\end{tikzpicture}

\end{document}
• It is always best to compose a MWE that illustrates the problem including the \documentclass so that those trying to help don't have to recreate it. Basically show us what you have tried... – Peter Grill Oct 7 '11 at 1:06
• I've written a simple package, that works solely by including the pgfplots package, that draws a variety of types of Riemann sums of either fixed width rectangles of any number or of any number of varying width rectangles in conjunction with a defined function. It also has routines for combining them on one graph with appropriate order of overlap. See my answer to Placing Coloured Rectangles on a Plot Using Points From the Plot Riemann Sums – Geoff Pointer Oct 31 '13 at 1:04

If you're happy with using the midpoint of the partitions for the approximation (instead of the maximum or minimum), you can relatively easily do this in PGFplots.

I've defined a new style integral=<lower>:<upper> that draws the rectangles of the approximate integral. You can set the number of partitions by setting integral segments=<number> (default is 3).

The style could easily be adjusted to use the left or right point of the partition for the approximation.

For example

\pgfset{declare function={f=-15*(x-5)+(x-5)^3+50;}}
red,
integral=2:6
] {f}

would yield Setting the number of partitions:

fill=yellow,
integral segments=10,
integral=2:8
] {f}; Here's the complete code:

\documentclass{article}

\usepackage{pgfplots}

\begin{document}

\pgfplotsset{
integral segments/.code={\pgfmathsetmacro\integralsegments{#1}},
integral segments=3,
integral/.style args={#1:#2}{
ybar interval,
domain=#1+((#2-#1)/\integralsegments)/2:#2+((#2-#1)/\integralsegments)/2,
samples=\integralsegments+1,
x filter/.code=\pgfmathparse{\pgfmathresult-((#2-#1)/\integralsegments)/2}
}
}

\begin{tikzpicture}[/pgf/declare function={f=-15*(x-5)+(x-5)^3+50;}]
\begin{axis}[
domain=0:10,
samples=100,
axis lines=middle
]
red,
integral=2:6
] {f};
\end{axis}
\end{tikzpicture}

\end{document}

Did you take a look at the tkz-fct package? It does whole bunch of stuff, including Riemann sums.

• Error message: "Plot data file 'riemann.tkzfct.table' not found". But i use TexLive 2011 and this is complete. – Regis da Silva Oct 7 '11 at 1:26
• @RegisdaSilva Could you edit your original post, and add an example showing what you did? – Torbjørn T. Oct 7 '11 at 1:37
• Do you have gnuplot installed? The beginning of the manual (altermundus.fr/downloads/packages/tkz-fct-screen.pdf) explains how the package uses gnuplot, and how to install it. – Jan Hlavacek Oct 7 '11 at 1:48
• The gnuplot is installed but I know that I am not using. Take a simple example and please tell me the steps (commands necessary) to generate the chart. I am using pdflatex. – Regis da Silva Oct 7 '11 at 2:12
• Apparently in texlive 2011, gnuplot is not on the list of programs allowed to use write18. You can run pdflatex with the --shell-escape option, it will override the restrictions and execute gnuplot for you. Or you can execute gnuplot manually between runs of pdflatex. In this case, you want to run gnuplot riemann.tkzfct.gnuplot. That should create the .table file. – Jan Hlavacek Oct 7 '11 at 3:14