definite_integrals

176 days ago by Ben_Trahan

outlinecolor = (0.5, 0.5, 0.6) fillcolor = (0.8, 0.8, 0.9) def leftsum(a,b,n,f): p = line([]) t = 0.000 width = (b-a)/n for i in range(0,n): t += (b-a)/n*f((i)*width+a) ppppoints = [(i*width+a, 0), ((i+1)*width+a, 0), ((i+1)*width+a, f((i)*width+a)), (i*width+a, f((i)*width+a))] p += polygon(ppppoints, rgbcolor=fillcolor) for j in range(0,4): p += line([ppppoints[j], ppppoints[(j+1)%4]], rgbcolor=outlinecolor) exact = N(integrate(f, a, b)) t=N(t) print "left sum: {0}\nexact: {1}\nerror: {2} ({3}%)\n".format(t, exact, t-exact, 100*(t-exact)/exact) p += plot(f, a, b, axes_labels=["$x$", "$y$"]) return p def rightsum(a,b,n,f): p = line([]) t = 0.000 width = (b-a)/n for i in range(0,n): t += (b-a)/n*f((i+1)*width+a) ppppoints = [(i*width+a, 0), ((i+1)*width+a, 0), ((i+1)*width+a, f((i+1)*width+a)), (i*width+a, f((i+1)*width+a))] p += polygon(ppppoints, rgbcolor=fillcolor) for j in range(0,4): p += line([ppppoints[j], ppppoints[(j+1)%4]], rgbcolor=outlinecolor) exact = N(integrate(f, a, b)) t=N(t) print "right sum: {0}\nexact: {1}\nerror: {2} ({3}%)\n".format(t, exact, t-exact, 100*(t-exact)/exact) p += plot(f, a, b, axes_labels=["$x$", "$y$"]) return p def trapezoidsum(a,b,n,f): p = line([]) t = 0.000 width = (b-a)/n for i in range(0,n): t += (b-a)/n*(f((i+1)*width+a) + f(i*width+a))/2.0 ppppoints = [(i*width+a, 0), ((i+1)*width+a, 0), ((i+1)*width+a, f((i+1)*width+a)), (i*width+a, f(i*width+a))] p += polygon(ppppoints, rgbcolor=fillcolor) for j in range(0,4): p += line([ppppoints[j], ppppoints[(j+1)%4]], rgbcolor=outlinecolor) exact = N(integrate(f, a, b)) t=N(t) print "trapezoid sum: {0}\nexact: {1}\nerror: {2} ({3}%)\n".format(t, exact, t-exact, 100*(t-exact)/exact) p += plot(f, a, b, axes_labels=["$x$", "$y$"]) return p def midpointsum(a,b,n,f): p = line([]) t = 0.000 width = (b-a)/n for i in range(0,n): t += (b-a)/n*f((i+0.5)*width+a) ppppoints = [(i*width+a, 0), ((i+1)*width+a, 0), ((i+1)*width+a, f((i+0.5)*width+a)), (i*width+a, f((i+0.5)*width+a))] p += polygon(ppppoints, rgbcolor=fillcolor) for j in range(0,4): p += line([ppppoints[j], ppppoints[(j+1)%4]], rgbcolor=outlinecolor) exact = N(integrate(f, a, b)) t=N(t) print "midpoint sum: {0}\nexact: {1}\nerror: {2} ({3}%)\n".format(t, exact, t-exact, 100*(t-exact)/exact) p += plot(f, a, b, axes_labels=["$x$", "$y$"]) return p def parabolicsum(a,b,n,f): p = line([]) t = 0.000 steps = n/2 width = (b-a)/n for i in range(0,steps): t += (b-a)/(3*n)*(f(2*i*width+a)+4*f((2*i+1)*width+a)+f((2*i+2)*width+a)) ppppoints = [(2*i*width+a, f((2*i)*width+a)), (2*i*width+a, 0), ((2*i+2)*width+a, 0), ((2*i+2)*width+a, f((2*i+2)*width+a)), ((2*i+1)*width+a, 0), ((2*i+1)*width+a, f((2*i+1)*width+a))] for j in range(0,3): p += line([ppppoints[j], ppppoints[(j+1)%4]], rgbcolor=outlinecolor) p += line([ppppoints[4], ppppoints[5]], rgbcolor=outlinecolor) xs = matrix(RR, 3, [(2*i*width+a)^2, (2*i*width+a), 1.0, ((2*i+1)*width+a)^2, ((2*i+1)*width+a), 1.0, ((2*i+2)*width+a)^2, ((2*i+2)*width+a), 1.0]) ys = vector(RR, [f(2*i*width+a), f((2*i+1)*width+a), f((2*i+2)*width+a)]) coeffs = xs\ys p += plot(coeffs[0]*x^2 + coeffs[1]*x + coeffs[2], 2*i*width+a, (2*i+2)*width+a, fill="axis", rgbcolor=outlinecolor, fillcolor=fillcolor) exact = N(integrate(f, a, b)) t=N(t) print "parabolic sum: {0}\nexact: {1}\nerror: {2} ({3}%)\n".format(t, exact, t-exact, 100*(t-exact)/exact) p += plot(f, a, b, axes_labels=["$x$", "$y$"]) return p 
       
f(x) = sin(x) a=-pi b=3*pi/2 n=6 leftsum(a,b, n, f).show() rightsum(a,b, n, f).show() midpointsum(a,b, n, f).show() trapezoidsum(a,b, n, f).show() parabolicsum(a,b, n, f).show() 
       
left sum: -0.196460542437491
exact: -1.00000000000000
error: 0.803539457562509 (-80.3539457562509%)


right sum: -1.50545748143324
exact: -1.00000000000000
error: -0.505457481433239 (50.5457481433239%)


midpoint sum: -1.07313130482598
exact: -1.00000000000000
error: -0.0731313048259796 (7.31313048259796%)


trapezoid sum: -0.850959011935364
exact: -1.00000000000000
error: 0.149040988064636 (-14.9040988064636%)


parabolic sum: -1.01836379168091
exact: -1.00000000000000
error: -0.0183637916809114 (1.83637916809114%)

left sum: -0.196460542437491
exact: -1.00000000000000
error: 0.803539457562509 (-80.3539457562509%)


right sum: -1.50545748143324
exact: -1.00000000000000
error: -0.505457481433239 (50.5457481433239%)


midpoint sum: -1.07313130482598
exact: -1.00000000000000
error: -0.0731313048259796 (7.31313048259796%)


trapezoid sum: -0.850959011935364
exact: -1.00000000000000
error: 0.149040988064636 (-14.9040988064636%)


parabolic sum: -1.01836379168091
exact: -1.00000000000000
error: -0.0183637916809114 (1.83637916809114%)