Lesson 9 of 18

Right Riemann Sum

Right Riemann Sum

Use the right endpoint of each subinterval:

i=1nf(a+ih)hwhere h=ban\sum_{i=1}^{n} f(a + i \cdot h) \cdot h \quad \text{where } h = \frac{b-a}{n}

a    a+h  a+2h       b
|----|----|----|...|
     ^    ^         ^
     right endpoints used

Left vs. Right vs. Exact

For 01x2dx=13\int_0^1 x^2\, dx = \frac{1}{3}:

Methodn=10n=100n=1000
Left0.2850.3280.332
Right0.3850.3380.334
Exact0.3330.3330.333

The left sum underestimates (for increasing ff) and the right sum overestimates. The true integral is sandwiched between them:

riemann_left(f,a,b,n)abf(x)dxriemann_right(f,a,b,n)\text{riemann\_left}(f, a, b, n) \leq \int_a^b f(x)\, dx \leq \text{riemann\_right}(f, a, b, n)

(assuming ff is increasing; swap inequality if decreasing)

Average of Left and Right

The average of the left and right Riemann sums is the trapezoidal rule (next lesson), which has O(h2)O(h^2) error.

Your Task

Implement double riemann_right(double (*f)(double), double a, double b, int n). The loop runs from i=1i=1 to i=ni=n (inclusive).

TCC compiler loading...
Loading...
Click "Run" to execute your code.