def MaxError(n): x = linspace(0,2*math.pi,40) f = array(map(fourierSum,x)) print f xx = linspace(0,2*math.pi,999) z = polint(x,f,xx,n) yy = z[0] dy = z[1] yacc = array(map(fourierSum,xx)) return abs(yacc-yy).max()
from numpy import * from matplotlib.pyplot import * import scipy.weave as weave from week0 import polint import scipy.special as sp def func_new (x): num = power(x,(1+sp.jv(0,x))) denom = sqrt(1-x*x) return num/denom xbig = arange(0.1,0.95,0.05) funcBigTable = array(map(func_new,xbig)) figure(0) plot(xbig,funcBigTable,'k',color = 'g') for n in range(3,8): x = linspace(0.3,0.7,n+1) y = map(func_new,x) z = polint(xbig,funcBigTable,x,n) figure(n) semilogy(x,abs(y-z[0]),'k') show()
from scipy import * from matplotlib.pyplot import * import scipy.weave as weave from week0 import polint xarr = linspace(0,1,5) yarr = sin(6*xarr*xarr) xx = linspace(-0.5,1.5,200) n = 4 z = polint(xarr,yarr,xx,n) yacc = sin(6*xx*xx) yy = z[0] dy = z[1] figure(0) plot(xx,yy,'ro',xx,yacc,'k') title("Interpolation by %dth order polynomial" % n ) figure(1) semilogy(xx,abs(yacc-yy),'ro',xx,dy,'k') title("Error plot") legend(["Actual Error","estimated error"]) show()
return sum def MaxError(n): x = linspace(0,2*math.pi,40) f = array(map(fourierSum,x)) print f xx = linspace(0,2*math.pi,999) z = polint(x,f,xx,n) yy = z[0] dy = z[1] yacc = array(map(fourierSum,xx)) return abs(yacc-yy).max() x = linspace(0,2*math.pi,40) f = array(map(fourierSum,x)) xx = linspace(0,2*math.pi,999) z = polint(x,f,xx,5) yy = z[0] dy = z[1] figure(0) plot(xx,yy,'ro') yacc = map(fourierSum,xx) figure(1) plot(xx,abs(dy),'k') n = range(3,21) maxArray = map(MaxError,n) figure(2) loglog(n,maxArray,'k') show()