Numerical Integration: Some Riemann sums
Left, right, and midpoint Riemann sums
We partition the interval \([a,b]\) into \(n\) equal parts: \[x_k= a+k h\] for \(k=1,2\ldots n\) with \[h=\frac{b-a}{n}\tiny.\] For the choice of tags, we distinguish the following three cases: choose for each sub-interval \([x_{k-1},x_k]\) always the
- left endpoint, \(\;\,\,s_k=a+(k-1)h\;\;\;\) (left Riemann sum)
- right endpoint, \(\;\!s_k=a+k h\qquad\;\;\;\) (right Riemann sum)
- midpoint, \(\,s_k=a+(k-\tfrac{1}{2})h\;\;\) (midpoint Riemann sum)
The Riemann sums can now be written as follows:
- left Riemann sum = \(\displaystyle h\sum_{k=1}^{n}f\bigl(a+(k-1) h\bigr)\)
- right Riemann sum = \(\displaystyle h\sum_{k=1}^{n}f\bigl(a+k h\bigr)\)
- midpoint Riemann sum = \(\displaystyle h\sum_{k=1}^{n}f\bigl(a+(k-\tfrac{1}{2}) h\bigr)\)
These sums are used to approximate the area under the curve \(f\). A visualisation of the Riemann sums is made available below for you to play with, so that you can get a better idea of the differing situations.
Programming task
Write a function Riemannsom(f,a,b,n,methode'left endpoint')
that calculates not only the left Riemann sum of the function \(f\) on the interval \([a,b]\) when divided into \(n\) subintervals, but also the right Riemann sum and midpoint Riemann sum, respectively with the option method='right endpoint'
or method='midpoint'
.
Apply your function with \(n=100\) in the following two cases:
- \(\displaystyle \int_0^1\frac{4}{x^2+1}\,\dd x=\pi\)
- \(\displaystyle \int_0^{\pi}\sin(x)\,\dd x=2\)