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