[Tex/LaTex] Lower and upper Riemann sums

pgfplotstikz-pgf

I am trying to create a few images for educational purposes. Therefore I need to be able to illustrate the area under various functions, using lower and upper riemann sums (rectangles)

Reason for not including an MWE is that I do not know which package or tools are best suited for the task, wheter it is tikz, pgf-plots, or something else. Although prefferably not gnuplot, as it is not working for me.

So my goal is to type in the function, specify the start and end value, upper or lower sum, number of rectangles, and produce something like below.

illustration

From another question, I was able to discover how to make middle riemann sums. But I absolutely must have lower and upper =(

\documentclass[10pt,a4paper]{minimal}
\usepackage{pgfplots}

\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{document}

\pagecolor{black}

\begin{center}
\begin{tikzpicture}[color=white,/pgf/declare function={f=3*e^(-x)*x^3;}]
\begin{axis}[
    domain=0:8.1,
    samples=100,
    axis lines=middle
]
\addplot [ultra thick] {f};
\addplot [
    white,
    integral segments=4,
    integral=0:8
] {f};
\end{axis}
\end{tikzpicture}
\end{center}

\end{document}

Best Answer

A solution with PSTricks. Run it with xelatex or `latex->dvips->ps2pdf

\documentclass{article}
\usepackage{pstricks-add}
\begin{document}

\psset{plotpoints=200,algebraic}
\begin{pspicture}(-0.5,-2.5)(10,3)
\psStep[linecolor=magenta,StepType=upper,
  fillstyle=solid,fillcolor=cyan!50](0,9){20}{sqrt(x)*sin(x)}
\psStep[linecolor=blue,fillstyle=solid,fillcolor=blue!30,
  opacity=0.4](0,9){20}{sqrt(x)*sin(x)}
\psaxes[labelFontSize=\scriptstyle]{->}(0,0)(0,-2.25)(10,3)
\psplot[linewidth=1.5pt]{0}{10}{sqrt(x)*sin(x)}
\end{pspicture}

\psset{yunit=1.25cm}
\begin{pspicture}(-0.5,-1.75)(10,1.5)
\psaxes[labelFontSize=\scriptstyle]{->}(0,0)(0,-1.5)(10,1.5)
\psStep[StepType=Riemann,fillstyle=solid,
  fillcolor=magenta!20](0,10){50}{sqrt(x)*cos(x)*sin(x)}
\psplot[linewidth=1.5pt]{0}{10}{sqrt(x)*cos(x)*sin(x)}
\end{pspicture}
\end{document}

enter image description here