Now it works as expected. To demonstrate the behavior I plotted two different functions.
How it works:
You have the following keys:
integral segments
number of the equal parts which the domain of the function is divided into;
integral samples
number of computation cycles for getting the maximum value in the given interval defined by integral segments
integral min
draw the lower Riemann sum
integral max
draw the upper Riemann sum
I tested the example with tikz 2.10 CSV. There is a known bug and I added a fix to the code. For more details have a look at this question.
Why am I getting the PGF Math Error: Unknown function `getargs'?
Another relevant answer was provided by @Christian Feuersänger:
How do I use pgfmathdeclarefunction to create define a new pgf function?
\documentclass[10pt,a4paper]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{
integral segments/.code={\pgfmathsetmacro\integralsegments{#1}},
integral segments=3,
integral samples/.code={\edef\integralsamples{#1}},
integral samples = 10,
integral min/.style args={#1:#2}{
ybar interval,
domain=#1:#2,
samples=\integralsegments+1,
x filter/.code={
\edef\lastx{\pgfmathresult}
\pgfmathresult
},%
y filter/.code={%
\pgfmathparse{(#2/(\integralsegments))/\integralsamples}%
\edef\tempstep{\pgfmathresult}%
\pgfmathparse{f(\lastx)}%
\edef\tempa{\pgfmathresult}%
\edef\tempb{\pgfmathresult}%
\foreach \x in {0,1,...,\integralsamples}%
{%
\pgfmathparse{f(\lastx+\x*\tempstep)}%
\xdef\tempb{\tempb,\pgfmathresult}%
}%
\pgfmathmin{\tempb}{\tempb}
},
},
integral max/.style args={#1:#2}{
ybar interval,
domain=#1:#2,
samples=\integralsegments+1,
x filter/.code={
\edef\lastx{\pgfmathresult}
\pgfmathresult
},%
y filter/.code={%
\pgfmathparse{(#2/(\integralsegments))/\integralsamples}%
\edef\tempstep{\pgfmathresult}%
\pgfmathparse{f(\lastx)}%
\edef\tempa{\pgfmathresult}%
\edef\tempb{\pgfmathresult}%
\foreach \x in {0,1,...,\integralsamples}%
{%
\pgfmathparse{f(\lastx+\x*\tempstep)}%
\xdef\tempb{\tempb,\pgfmathresult}%
}%
\pgfmathmax{\tempb}{\tempb}
},
},
}
\makeatletter
%see http://tex.stackexchange.com/questions/9722/why-am-i-getting-the-pgf-math-error-unknown-function-getargs
\def\pgfmathmax#1#2{%
% \pgfmathparse{getargs(#1,#2)}%
\pgfmathparse{#1,#2}%
\expandafter\pgfmathmax@\expandafter{\pgfmathresult}%
}
\def\pgfmathmin#1#2{%
\pgfmathparse{#1,#2}%
\expandafter\pgfmathmin@\expandafter{\pgfmathresult}%
}
%see http://tex.stackexchange.com/questions/15435/how-do-i-use-pgfmathdeclarefunction-to-create-define-a-new-pgf-function
\makeatother
\begin{document}
\begin{tikzpicture}
\pgfset{declare function={f(\x)=3*exp(-(\x))*(\x)^3+1;}}
\begin{axis}[
domain=0:8.1,
samples=100,
axis lines=middle
]
\addplot [ultra thick] {f(x)};
\addplot [
red,
integral segments=4,
integral min=0:8
] {f(x)};
\end{axis}
\end{tikzpicture}
\hfill
\begin{tikzpicture}
\pgfset{declare function={f(\x)=3*exp(-(\x))*(\x)^3+1;}}
\begin{axis}[
domain=0:8.1,
samples=100,
axis lines=middle
]
\addplot [ultra thick] {f(x)};
\addplot [
blue,
integral segments=5,
integral max=0:8
] {f(x)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\pgfset{declare function={f(\x)=sin(2*deg(\x))*exp(0.1*\x)+2;}}
\begin{axis}[
domain=0:8.1,
samples=100,
axis lines=middle
]
\addplot [ultra thick] {f(x)};
\addplot [
red,
integral segments=4,
integral min=0:8
] {f(x)};
\end{axis}
\end{tikzpicture}
\hfill
\begin{tikzpicture}
\pgfset{declare function={f(\x)=sin(2*deg(\x))*exp(0.1*\x)+2;}}
\begin{axis}[
domain=0:8.1,
samples=100,
axis lines=middle
]
\addplot [ultra thick] {f(x)};
\addplot [
blue,
integral segments=5,
integral max=0:8
] {f(x)};
\end{axis}
\end{tikzpicture}
\end{document}

TEST CODE to handle functions with f(x)<0:
\documentclass[10pt,a4paper]{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfplotsset{
integral segments/.code={\pgfmathsetmacro\integralsegments{#1}},
integral segments=3,
integral samples/.code={\edef\integralsamples{#1}},
integral samples = 10,
integral min/.style args={#1:#2}{
ybar interval,
domain=#1:#2,
samples=\integralsegments+1,
x filter/.code={
\edef\lastx{\pgfmathresult}
\pgfmathresult
},%
y filter/.code={%
\pgfmathparse{(#2/(\integralsegments))/\integralsamples}%
\edef\tempstep{\pgfmathresult}%
\pgfmathparse{f(\lastx)}%
\edef\tempa{\pgfmathresult}%
\edef\tempb{\pgfmathresult}%
\foreach \x in {0,1,...,\integralsamples}%
{%
\pgfmathparse{f(\lastx+\x*\tempstep)}%
\xdef\tempb{\tempb,\pgfmathresult}%
}%
\pgfmathmin{\tempb}{\tempb}
\let\savepgfmathresult\pgfmathresult
\pgfmathgreater{\pgfmathresult}{0}
\ifdim\pgfmathresult pt> 0 pt \relax
\let\pgfmathresult\savepgfmathresult
\else
\pgfmathparse{f(\lastx)}%
\edef\tempa{\pgfmathresult}%
\edef\tempb{\pgfmathresult}%
\foreach \x in {0,1,...,\integralsamples}%
{%
\pgfmathparse{f(\lastx+\x*\tempstep)}%
\xdef\tempb{\tempb,\pgfmathresult}%
}%
\pgfmathmax{\tempb}{\tempb}
\fi
},
},
integral max/.style args={#1:#2}{
ybar interval,
domain=#1:#2,
samples=\integralsegments+1,
x filter/.code={
\edef\lastx{\pgfmathresult}
\pgfmathresult
},%
y filter/.code={%
\pgfmathparse{(#2/(\integralsegments))/\integralsamples}%
\edef\tempstep{\pgfmathresult}%
\pgfmathparse{f(\lastx)}%
\edef\tempa{\pgfmathresult}%
\edef\tempb{\pgfmathresult}%
\foreach \x in {0,1,...,\integralsamples}%
{%
\pgfmathparse{f(\lastx+\x*\tempstep)}%
\xdef\tempb{\tempb,\pgfmathresult}%
}%
\pgfmathmax{\tempb}{\tempb,0}
\let\savepgfmathresult\pgfmathresult
% \pgfmathparse{ifthenelse(\pgfmathresult>=0,1,0)}
\pgfmathgreater{\pgfmathresult}{0}
\ifdim\pgfmathresult pt> 0 pt \relax
\let\pgfmathresult\savepgfmathresult
\else
\pgfmathparse{f(\lastx)}%
\edef\tempa{\pgfmathresult}%
\edef\tempb{\pgfmathresult}%
\foreach \x in {0,1,...,\integralsamples}%
{%
\pgfmathparse{f(\lastx+\x*\tempstep)}%
\xdef\tempb{\tempb,\pgfmathresult}%
}%
\pgfmathmin{\tempb}{\tempb}
\fi
},
},
}
\makeatletter
%see http://tex.stackexchange.com/questions/9722/why-am-i-getting-the-pgf-math-error-unknown-function-getargs
\def\pgfmathmax#1#2{%
% \pgfmathparse{getargs(#1,#2)}%
\pgfmathparse{#1,#2}%
\expandafter\pgfmathmax@\expandafter{\pgfmathresult}%
}
\def\pgfmathmin#1#2{%
\pgfmathparse{#1,#2}%
\expandafter\pgfmathmin@\expandafter{\pgfmathresult}%
}
%see http://tex.stackexchange.com/questions/15435/how-do-i-use-pgfmathdeclarefunction-to-create-define-a-new-pgf-function
\makeatother
\begin{document}
\begin{tikzpicture}
\pgfset{declare function={f(\x)=3*exp(-(\x))*(\x)^3+1;}}
\begin{axis}[
domain=0:8.1,
samples=100,
axis lines=middle
]
\addplot [ultra thick] {f(x)};
\addplot [
red,
integral segments=4,
integral min=0:8
] {f(x)};
\end{axis}
\end{tikzpicture}
\hfill
\begin{tikzpicture}
\pgfset{declare function={f(\x)=3*exp(-(\x))*(\x)^3+1;}}
\begin{axis}[
domain=0:8.1,
samples=100,
axis lines=middle
]
\addplot [ultra thick] {f(x)};
\addplot [
blue,
integral segments=5,
integral max=0:8
] {f(x)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\pgfset{declare function={f(\x)=sin(2*deg(\x))*exp(0.1*\x)+2;}}
\begin{axis}[
domain=0:8.1,
samples=100,
axis lines=middle
]
\addplot [ultra thick] {f(x)};
\addplot [
red,
integral segments=4,
integral min=0:8
] {f(x)};
\end{axis}
\end{tikzpicture}
\hfill
\begin{tikzpicture}
\pgfset{declare function={f(\x)=sin(2*deg(\x))*exp(0.1*\x)+2;}}
\begin{axis}[
domain=0:8.1,
samples=100,
axis lines=middle
]
\addplot [ultra thick] {f(x)};
\addplot [
blue,
integral segments=5,
integral max=0:8,
] {f(x)};
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\pgfset{declare function={f(\x)=sqrt(\x)*cos(deg(\x))*sin(deg(\x));}}
\begin{axis}[
domain=0:8.1,
samples=100,
axis lines=middle
]
\addplot [ultra thick] {f(x)};
\addplot [
red,
integral segments=30,
integral min=0:8
] {f(x)};
\end{axis}
\end{tikzpicture}
\hfill
\begin{tikzpicture}
\pgfset{declare function={f(\x)=sqrt(\x)*cos(deg(\x))*sin(deg(\x));}}
\begin{axis}[
domain=0:8.1,
samples=100,
axis lines=middle
]
\addplot [,ultra thick] {f(x)};
\addplot [
blue,
integral segments=15,
integral max=0:8
] {1};
\end{axis}
\end{tikzpicture}
\end{document}