Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
6697 views

The purpose of this worksheet is to help illustrate the concept of Riemann sum.

It allows you to compare, both numerically and graphically, abf(x)dxandi=1nf(xi)(xixi1)=(ban)i=1nf(xi) \int_a^bf(x)dx\qquad \mbox{and} \qquad \sum_{i=1}^nf(x_i^*)(x_i-x_{i-1})=\left(\frac{b-a}{n}\right)\sum_{i=1}^nf(x_i^*) namely the definite integral of any function ff over any interval [a,b][a,b]and its left (i.e.  xi=xi1)\left(\mbox{i.e.}\;x_i^*=x_{i-1}\right) / right (i.e.  xi=xi)\left(\mbox{i.e.}\;x_i^*=x_i\right) / middle (i.e.  xi=(xi1+xi)/2)\left(\mbox{i.e.}\;x_i^*=(x_{i-1}+x_i)/2\right) Riemann sum with respect to the partition xi=a+i(ban)i=0,1,,nx_i=a+i\left(\frac{b-a}{n}\right)\qquad i=0, 1, \ldots, n of [a,b][a,b] into nn subintervals of equal width.

First draft

In the cell below:

  • Make your own choices for f(x)f(x), aa, bb and nn.

  • Set t=0t=0 if you want a left Riemann sum, t=1t=1 for a right one, and t=0.5t=0.5 for a middle one.

  • Evaluate and see the output.

f(x) = x^3-5*x+2 ### The function. a = -1 ### The lower bound. b = 2 ### The upper bound. n = 18 ### The number of rectangles. t = 1 ### 0 for a left Riemann sum, 1 for a right one, 0.5 for a middle one. ############################################################################################### ##########################Do not edit below this line.######################################### ############################################################################################### I = integral(f(x), x, a, b).n() delta = (b-a)/n; tdelta = t*delta; xk = a; L = []; S = 0 for k in range(n): L = L + [(xk, 0)] y = f(xk+tdelta) S = S + y L = L + [(xk,y)] xk = xk + delta L = L + [(xk, y)] S = delta*S.n() pretty_print('Integral = %s'%I) pretty_print('Riemann Sum = %s'%S) L = L + [(xk,0)] G = plot(f(x), (x, a-(b-a)/20, b+(b-a)/20), color = 'red', thickness = 1) G = G + plot(f(x), (x, a, b), color = 'red', thickness = 1, fill = true, fillcolor = 'grey') G = G + polygon(L, edgecolor = 'black', rgbcolor = (t,t^2,1-t)) G.show(aspect_ratio = 'automatic')
Integral = 2.25000000000000
Riemann Sum = 1.77083333333333

Using the interactive capabilities of SageMath

Evaluating the following cell will do the same thing as above, with some of the user-friendly options provided by @interact. See http://wiki.sagemath.org/interact/ for more examples.

@interact def _(f=input_box(default=x^3-5*x+2), a=input_box(default=-1), b=input_box(default=2), n=input_box(default=15), Method=['Left', 'Right', 'Middle']): if Method=='Left': t=0 elif Method=='Right': t=1 else: t=0.5 f(x)=f I = integral(f(x), x, a, b).n() delta = (b-a)/n; tdelta = t*delta; xk = a; L = []; S = 0 for k in range(n): L = L + [(xk, 0)] y = f(xk+tdelta) S = S + y L = L + [(xk,y)] xk = xk + delta L = L + [(xk, y)] S = delta*S.n() pretty_print('Integral = %s'%I) pretty_print('Riemann Sum = %s'%S) L = L + [(xk,0)] G = plot(f(x), (x, a-(b-a)/20, b+(b-a)/20), color = 'red', thickness = 1) G = G + plot(f(x), (x, a, b), color = 'red', thickness = 1, fill = true, fillcolor = 'grey') G = G + polygon(L, edgecolor = 'black', rgbcolor = (t,t^2,1-t)) G.show(aspect_ratio = 'automatic')
Interact: please open in CoCalc

Sharing our interact with the help of SageMathCell

Entering the above code in SageMathCell allows us to share this interact easily. Here it is.