# Riemann sum

> Eight left-endpoint rectangles under a curve, approximating its integral. A Functions and calculus example with its LaTeX source.

A Functions and calculus example in the TeXeR TikZ library: https://texer.org/library/calculus/riemann-sum

Package: [Functions and calculus](https://texer.org/library/calculus.md), on CTAN at https://ctan.org/pkg/pgf.

Tags: Riemann sum, integral, approximation

## Source

```latex
\usetikzlibrary{arrows.meta}

\begin{tikzpicture}[>={Stealth}, scale=1.1]
  \foreach \i in {0,...,7} {
    \pgfmathsetmacro\x{0.5 + 0.5*\i}
    \pgfmathsetmacro\h{2 + sin(deg(\x))}
    \draw[fill=orange!25, draw=orange!70!black] (\x,0) rectangle ++(0.5,\h);
  }
  \draw[->] (-0.2,0) -- (5,0) node[right] {$x$};
  \draw[->] (0,-0.2) -- (0,3.4) node[above] {$y$};
  \draw[very thick, blue!70!black, domain=0.2:4.7, samples=60, smooth]
  plot (\x, {2 + sin(deg(\x))});
  \node[below] at (0.5,0) {$a$};
  \node[below] at (4.5,0) {$b$};
  \node[font=\small, align=center] at (2.5,-0.9) {$\displaystyle\sum_{i=0}^{7} f(x_i)\,\Delta x \approx \int_a^b f(x)\,dx$};
\end{tikzpicture}
```

## As a complete document

The same example in a document that compiles on its own with pdfLaTeX:

```latex
\documentclass[margin=3mm]{standalone}
\usepackage{tikz}
\usepackage{xcolor}
\usepackage{amsmath}
\usepackage{amssymb}
\usetikzlibrary{arrows.meta}

\begin{document}

\begin{tikzpicture}[>={Stealth}, scale=1.1]
  \foreach \i in {0,...,7} {
    \pgfmathsetmacro\x{0.5 + 0.5*\i}
    \pgfmathsetmacro\h{2 + sin(deg(\x))}
    \draw[fill=orange!25, draw=orange!70!black] (\x,0) rectangle ++(0.5,\h);
  }
  \draw[->] (-0.2,0) -- (5,0) node[right] {$x$};
  \draw[->] (0,-0.2) -- (0,3.4) node[above] {$y$};
  \draw[very thick, blue!70!black, domain=0.2:4.7, samples=60, smooth]
  plot (\x, {2 + sin(deg(\x))});
  \node[below] at (0.5,0) {$a$};
  \node[below] at (4.5,0) {$b$};
  \node[font=\small, align=center] at (2.5,-0.9) {$\displaystyle\sum_{i=0}^{7} f(x_i)\,\Delta x \approx \int_a^b f(x)\,dx$};
\end{tikzpicture}
\end{document}
```
