CRL: Calc2 1.3 Reimann Sums MRG 2012.0228

3507 days ago by calcpage123

# Practice 1.3.3 python lists and sums show(1/k for k in range(1,5)) show(sum(1/k for k in range(1,5)).n()) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[1, \frac{1}{2}, \frac{1}{3}, \frac{1}{4}\right]
\newcommand{\Bold}[1]{\mathbf{#1}}2.08333333333333
\newcommand{\Bold}[1]{\mathbf{#1}}\left[1, \frac{1}{2}, \frac{1}{3}, \frac{1}{4}\right]
\newcommand{\Bold}[1]{\mathbf{#1}}2.08333333333333
# pp18-20 
       
# find left,right and trap Riemann sums for integrate(x^2,x,1,2) with 4 rectangles (fractionally): f(x)=x^2 show(f(x)) a=1 b=2 n=4 Dx=(b-a)/n show(Dx) Lsum = sum([f(a+i*Dx)*Dx for i in range(n)]) show(Lsum) show(integrate(f(x),x,1,2)) Rsum = sum([f(a+(i+1)*Dx)*Dx for i in range(n)]) show(Rsum) Trap = (Lsum+Rsum)/2 show(Trap) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}x^{2}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{4}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{63}{32}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{7}{3}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{87}{32}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{75}{32}
\newcommand{\Bold}[1]{\mathbf{#1}}x^{2}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{4}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{63}{32}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{7}{3}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{87}{32}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{75}{32}
# find left, right and trap Riemann sums for integrate(x^2,x,1,2) with 4 rectangles (decimals): f(x)=x^2 show(f(x)) a=1 b=2 n=4 Dx=(b-a)/n show(Dx) Lsum = sum([f(a+i*Dx)*Dx for i in range(n)]) show(Lsum.n()) show(integrate(f(x),x,1,2).n()) Rsum = sum([f(a+(i+1)*Dx)*Dx for i in range(n)]) show(Rsum.n()) Trap = (Lsum+Rsum)/2 show(Trap.n()) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}x^{2}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{4}
\newcommand{\Bold}[1]{\mathbf{#1}}1.96875000000000
\newcommand{\Bold}[1]{\mathbf{#1}}2.33333333333333
\newcommand{\Bold}[1]{\mathbf{#1}}2.71875000000000
\newcommand{\Bold}[1]{\mathbf{#1}}2.34375000000000
\newcommand{\Bold}[1]{\mathbf{#1}}x^{2}
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{4}
\newcommand{\Bold}[1]{\mathbf{#1}}1.96875000000000
\newcommand{\Bold}[1]{\mathbf{#1}}2.33333333333333
\newcommand{\Bold}[1]{\mathbf{#1}}2.71875000000000
\newcommand{\Bold}[1]{\mathbf{#1}}2.34375000000000
#using python functions! def myLsum(g,a,b,n): Dx=(b-a)/n return sum([g(a+i*Dx)*Dx for i in range(n)]) myLsum(1/x,1,5,5).n() 
       
\newcommand{\Bold}[1]{\mathbf{#1}}1.97790706026000
\newcommand{\Bold}[1]{\mathbf{#1}}1.97790706026000
def myRsum(g,a,b,n): Dx=(b-a)/n return sum([g(a+(i+1)*Dx)*Dx for i in range(n)]) myRsum(1/x,1,5,5).n() 
       
\newcommand{\Bold}[1]{\mathbf{#1}}1.33790706026000
\newcommand{\Bold}[1]{\mathbf{#1}}1.33790706026000
def myTsum(g,a,b,n): return (myLsum(g,a,b,n)+myRsum(g,a,b,n))/2 myTsum(1/x,1,5,5).n() 
       
\newcommand{\Bold}[1]{\mathbf{#1}}1.65790706026000
\newcommand{\Bold}[1]{\mathbf{#1}}1.65790706026000
sizes=[5,10,20,100,1000] sizes 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[5, 10, 20, 100, 1000\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[5, 10, 20, 100, 1000\right]
a=1 b=5 g(x)=1/x table=[[n,(b-a)/n.n(digits=4),myLsum(g,a,b,n).n(digits=4),myTsum(g,a,b,n).n(digits=4),myRsum(g,a,b,n).n(digits=4)] for n in sizes] table 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[5, 0.8000, 1.978, 1.658, 1.338\right], \left[10, 0.4000, 1.782, 1.622, 1.462\right], \left[20, 0.2000, 1.693, 1.613, 1.533\right], \left[100, 0.04000, 1.626, 1.610, 1.594\right], \left[1000, 0.004000, 1.611, 1.609, 1.608\right]\right]
\newcommand{\Bold}[1]{\mathbf{#1}}\left[\left[5, 0.8000, 1.978, 1.658, 1.338\right], \left[10, 0.4000, 1.782, 1.622, 1.462\right], \left[20, 0.2000, 1.693, 1.613, 1.533\right], \left[100, 0.04000, 1.626, 1.610, 1.594\right], \left[1000, 0.004000, 1.611, 1.609, 1.608\right]\right]
def myMsum(g,a,b,n): Dx=(b-a)/n return sum([g(a+(i+0.5)*Dx)*Dx for i in range(n)]) myMsum(1/x,1,5,5).n() 
       
\newcommand{\Bold}[1]{\mathbf{#1}}1.58617096099934
\newcommand{\Bold}[1]{\mathbf{#1}}1.58617096099934
def mySsum(g,a,b,n): return (2*myMsum(g,a,b,n)+myTsum(g,a,b,n))/3 mySsum(1/x,1,5,5) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}1.61008299408622
\newcommand{\Bold}[1]{\mathbf{#1}}1.61008299408622
print "n\tLsum\t\tTsum\t\tSsum\t\tMsum\t\tRsum" for n in sizes: print n,"\t",myLsum(g,a,b,n).n(digits=7),"\t",myTsum(g,a,b,n).n(digits=7),"\t",mySsum(g,a,b,n).n(digits=7),"\t",myMsum(g,a,b,n).n(digits=7),"\t",myRsum(g,a,b,n).n(digits=7) 
       
n	Lsum		Tsum		Ssum		Msum		Rsum
5 	1.977907 	1.657907 	1.610083 	1.586171 	1.337907
10 	1.782039 	1.622039 	1.609487 	1.603211 	1.462039
20 	1.692625 	1.612625 	1.609441 	1.607849 	1.532625
100 	1.625566 	1.609566 	1.609438 	1.609374 	1.593566
1000 	1.611039 	1.609439 	1.609438 	1.609437 	1.607839
n	Lsum		Tsum		Ssum		Msum		Rsum
5 	1.977907 	1.657907 	1.610083 	1.586171 	1.337907
10 	1.782039 	1.622039 	1.609487 	1.603211 	1.462039
20 	1.692625 	1.612625 	1.609441 	1.607849 	1.532625
100 	1.625566 	1.609566 	1.609438 	1.609374 	1.593566
1000 	1.611039 	1.609439 	1.609438 	1.609437 	1.607839
show(g(x)) show(integrate(g(x),x)) show(integrate(g(x),x,1,5)) show(integrate(g(x),x,1,5).n()) 
       
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{x}
\newcommand{\Bold}[1]{\mathbf{#1}}\log\left(x\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\log\left(5\right)
\newcommand{\Bold}[1]{\mathbf{#1}}1.60943791243410
\newcommand{\Bold}[1]{\mathbf{#1}}\frac{1}{x}
\newcommand{\Bold}[1]{\mathbf{#1}}\log\left(x\right)
\newcommand{\Bold}[1]{\mathbf{#1}}\log\left(5\right)
\newcommand{\Bold}[1]{\mathbf{#1}}1.60943791243410