Riemann Sum

947 days ago by jhlee2chn

var('i') f(x)=1/sqrt(1+4*x^2) sum(0.2*f(0.2*i-0.1),i,1,10) 
       
1.04754618408
1.04754618408
html("<i> <b> Approximate Integration via Riemann Sums: Riemann Sum을 이용하여 적분의 근사값을 구하는 과정 시각화 <p></p> </b>" ) html("<i> <b> Posted by Jeff Denny on 06 Jul 2012<p></p> </b>" ) @interact def demo(g=sin(x),a=0,b=pi,n=slider(0,20,1,6),method=selector(['Left', 'Right', 'Midpoint', 'Trapezoid'], nrows=1, label="Method"),fillcurve=('Fill area under curve',False)): var('k') f(x)=g dx = (b-a)/n pts = [a+i*dx for i in range(0,n+1)] p=Graphics() if method=='Left': for i in range(0,n): h=f(pts[i]) p+=polygon([(pts[i],0),(pts[i],h),(pts[i+1],h),(pts[i+1],0)],fill=False) approx=dx*add([f(pts[i]) for i in range(0,n)]) if method=='Right': for i in range(0,n): h=f(pts[i+1]) p+=polygon([(pts[i],0),(pts[i],h),(pts[i+1],h),(pts[i+1],0)],fill=False) approx=dx*add([f(pts[i]) for i in range(1,n+1)]) if method=='Midpoint': midpts=[a+1/2*dx+i*dx for i in range(0,n+1)] for i in range(0,n): h=f(midpts[i]) p+=polygon([(pts[i],0),(pts[i],h),(pts[i+1],h),(pts[i+1],0)],fill=False) approx=dx*add([f(midpts[i]) for i in range(0,n)]) if method=='Trapezoid': for i in range(0,n): p+=polygon([(pts[i],0),(pts[i],f(pts[i])),(pts[i+1],f(pts[i+1])),(pts[i+1],0)],fill=False) approx=dx*add([(f(pts[i])+f(pts[i+1]))/2 for i in range(0,n)]) p+=plot(f(x),(x,a,b),color='red',fill=fillcurve) show(p) actual=integral(f(x),x,a,b).n() approx=approx.n() html(r'$\int_{%s}^{%s} f(x)\, dx = %s$' % (latex(a) , latex(b),actual)) html(method +' Approximation = %s'% approx) html('Error = %s'% abs(actual-approx)) 
       
Approximate Integration via Riemann Sums: Riemann Sum을 이용하여 적분의 근사값을 구하는 과정 시각화

Posted by Jeff Denny on 06 Jul 2012

Method 
Fill area under curve 

Click to the left again to hide and once more to show the dynamic interactive window