Riemann Sum

185 days ago by xiehongye

Consider the function f(x)=x^2+1 on the interval [0,4]

First lets approximate the area under the curve by using n=8 rectangles of width \Delta x=\frac{4-0}{8}=\frac{1}{2}
with using the left endpoints, midpoints and right endpoints of each subinterval.

 
       
 
       
%hide html('<h1>Left endpoints with $n=8$ rectangles</h1>') riemann_sum((x^2+1).function(x),0,4,8,0) 
       

Left endpoints with n=8 rectangles

Traceback (click to the left of this block for traceback) ... NameError: name 'riemann_sum' is not defined

Left endpoints with n=8 rectangles

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_2.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("aHRtbCgnPGgxPkxlZnQgZW5kcG9pbnRzIHdpdGggJG49OCQgcmVjdGFuZ2xlczwvaDE+JykKcmllbWFubl9zdW0oKHheMisxKS5mdW5jdGlvbih4KSwwLDQsOCwwKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmpHhMP8t/___code___.py", line 4, in <module> exec compile(u'riemann_sum((x**_sage_const_2 +_sage_const_1 ).function(x),_sage_const_0 ,_sage_const_4 ,_sage_const_8 ,_sage_const_0 ) File "", line 1, in <module> NameError: name 'riemann_sum' is not defined
%hide html('<h1>Midpoints with $n=8$ rectangles</h1>') riemann_sum((x^2+1).function(x),0,4,8,1/2) 
       

Midpoints with n=8 rectangles

Traceback (click to the left of this block for traceback) ... NameError: name 'riemann_sum' is not defined

Midpoints with n=8 rectangles

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_3.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("aHRtbCgnPGgxPk1pZHBvaW50cyB3aXRoICRuPTgkIHJlY3RhbmdsZXM8L2gxPicpCnJpZW1hbm5fc3VtKCh4XjIrMSkuZnVuY3Rpb24oeCksMCw0LDgsMS8yKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmpbrsrZc/___code___.py", line 4, in <module> exec compile(u'riemann_sum((x**_sage_const_2 +_sage_const_1 ).function(x),_sage_const_0 ,_sage_const_4 ,_sage_const_8 ,_sage_const_1 /_sage_const_2 ) File "", line 1, in <module> NameError: name 'riemann_sum' is not defined
%hide html('<h1>Right endpoints with $n=8$ rectangles</h1>') riemann_sum((x^2+1).function(x),0,4,8,1) 
       

Right endpoints with n=8 rectangles

Traceback (click to the left of this block for traceback) ... NameError: name 'riemann_sum' is not defined

Right endpoints with n=8 rectangles

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_4.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("aHRtbCgnPGgxPlJpZ2h0IGVuZHBvaW50cyB3aXRoICRuPTgkIHJlY3RhbmdsZXM8L2gxPicpCnJpZW1hbm5fc3VtKCh4XjIrMSkuZnVuY3Rpb24oeCksMCw0LDgsMSk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmpPWMOSR/___code___.py", line 4, in <module> exec compile(u'riemann_sum((x**_sage_const_2 +_sage_const_1 ).function(x),_sage_const_0 ,_sage_const_4 ,_sage_const_8 ,_sage_const_1 ) File "", line 1, in <module> NameError: name 'riemann_sum' is not defined

Notice that in this case the left endpoint Riemann sum will always be less than than the actual area
since the left hand endpoint is the minimum of the function in each subinterval.
The Riemann sum which uses the minimum point of each interval is called the Lower Sum. On the other hand the right endpoint
Riemann sum will always be greater than the actual area, which is called the Upper sum. Thus

21.5\leq A \leq 29.5
 

If we add more rectangles we get a far better approximation, so lets look at the Upper and Lower sums in the case of n=80 rectangles.

%hide html('<h1>Left endpoints with $n=80$ rectangles</h1>') riemann_sum((x^2+1).function(x),0,4,80,0) 
       

Left endpoints with n=80 rectangles

Traceback (click to the left of this block for traceback) ... NameError: name 'riemann_sum' is not defined

Left endpoints with n=80 rectangles

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_5.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("aHRtbCgnPGgxPkxlZnQgZW5kcG9pbnRzIHdpdGggJG49ODAkIHJlY3RhbmdsZXM8L2gxPicpCnJpZW1hbm5fc3VtKCh4XjIrMSkuZnVuY3Rpb24oeCksMCw0LDgwLDAp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmpDDD8Sv/___code___.py", line 4, in <module> exec compile(u'riemann_sum((x**_sage_const_2 +_sage_const_1 ).function(x),_sage_const_0 ,_sage_const_4 ,_sage_const_80 ,_sage_const_0 ) File "", line 1, in <module> NameError: name 'riemann_sum' is not defined
%hide html('<h1>Right endpoints with $n=80$ rectangles.</h1>') riemann_sum((x^2+1).function(x),0,4,80,1) 
       

Right endpoints with n=80 rectangles.

Traceback (click to the left of this block for traceback) ... NameError: name 'riemann_sum' is not defined

Right endpoints with n=80 rectangles.

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_6.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("aHRtbCgnPGgxPlJpZ2h0IGVuZHBvaW50cyB3aXRoICRuPTgwJCByZWN0YW5nbGVzLjwvaDE+JykKcmllbWFubl9zdW0oKHheMisxKS5mdW5jdGlvbih4KSwwLDQsODAsMSk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py")) File "", line 1, in <module> File "/tmp/tmpjSKd6J/___code___.py", line 4, in <module> exec compile(u'riemann_sum((x**_sage_const_2 +_sage_const_1 ).function(x),_sage_const_0 ,_sage_const_4 ,_sage_const_80 ,_sage_const_1 ) File "", line 1, in <module> NameError: name 'riemann_sum' is not defined

With more rectangles we get a better approximation, thus we now have

24.935A25.735000 

The more rectangles we add the better area approximation we get.

With n=1,000 rectangles:

25.301344\leq A\leq 25.365344

With n=10,000 rectangles:

25.330133\leq A\leq25.336533

With n=1,000,000 rectangles:

25.333301\leq A\leq25.333365

Thus the actual area can be found by taking the limit of n\to\infty. This is called the defnite integral:

%hide I = integral(x^2+1,x,0,4) html('<h2>$\\int_0^4 f(x)dx =\\lim_{n\\to\\infty}\\sum_{i=1}^{n}f(x_i)\Delta x = %s = %s' % (I,I.n(30))) 
       

\int_0^4 f(x)dx =\lim_{n\to\infty}\sum_{i=1}^{n}f(x_i)\Delta x = 76/3 = 25.333333

\int_0^4 f(x)dx =\lim_{n\to\infty}\sum_{i=1}^{n}f(x_i)\Delta x = 76/3 = 25.333333

%hide # Custom Function Definitions def riemann_sum(f,a,b,n,p): dx = (b-a)/n tp = p*dx P = line([(a,0),(b,0)]) S = 0 for i in range(0,n): P += line([(a+i*dx,0),(a+i*dx,f(a+i*dx+tp))]) P += line([(a+i*dx,f(a+i*dx+tp)),(a+(i+1)*dx,f(a+i*dx+tp))]) P += line([(a+(i+1)*dx,f(a+i*dx+tp)),(a+(i+1)*dx,0)]) P += line([(a+i*dx+tp,0),(a+i*dx+tp,f(a+i*dx+tp))],linestyle="--",color="green") S += f(a+i*dx+tp)*dx P += plot(f(x),(x,a,b),thickness=2,color="red") show(P) html('<h2>$\\sum_{i=1}^{%s} f(x_i)\\Delta x = %s$</h2>' % (n,S.n(30))) return def rsum(f,a,b,n,p): dx = (b-a)/n tp = p*dx S = 0 for i in range(0,n): S += f(a+i*dx+tp)*dx return S