#hw8.txt Cole Franks 20/19/2014 read(`/Users/Cole/C8.txt`): ### problem 1. #ApplyNCg(f,x,n,a,b) integrates f by changing #variables to make the limits of integration 0 to 1 #and then applying ApplyNC. ApplyNCg:=proc(f,x,n,a,b) local y, g: g:=(b-a)*subs(x = a + (b - a)*y, f): ApplyNC(g, y, n): end: ### problem 2. #ApplyNCgg(f,x,n,a,b, K) chops the interval up into K equal intervals #then applies ApplyNCg to each interval. ApplyNCgg:=proc(f,x,n,a,b, K): add(ApplyNCg(f,x,n,a + i*(b-a)/K, a + (i+1)*(b-a)/K), i=0..K-1): end: ### problem 3. BestNC:=proc(f,x,M) local i, s, B, integ: option remember: s:=10: B:=1: Digits:=40: integ:=evalf(int(f, x=0..1)): for i from 1 to M do if type(M/i, integer) then if evalf(abs(ApplyNCgg(f, x, i, 0, 1, M/i) - integ)) < s then s:=evalf(abs(ApplyNCgg(f, x, i, 0, 1, M/i) - integ)): B:=i: print(s,B): fi: fi: od: [B,M/B]: end: #For each function, n=32, K=1 was best. ##### Problem 4 ApplyGauss:=proc(f, x, n) local L: L:=evalf(FindGauss(n)): add(subs(x=L[1][i],f)*L[2][i], i=1..nops(L[1])): end: #### Problem 5 #I only did 1..4 because my computer was hanging for n=5. #function: #f=1/(1+x^2) #errors (Gauss, NC) # 0.0146018365, 0.0353981635 # 0.0014870823, 0.0020648302 # 0.0001311284, 0.0007827789 # HFloat(4.812757779637522e-6), 0.0001312483 #f=1/(1+x^4) # 0.0742034835, 0.1169729871 # 0.0074504997, 0.0104779933 # 0.0005454789, 0.0040981721 # 1.7329915309449717e-5, 0.0005484385 #f=exp(x) # 0.069560557, 0.1408590860 # 0.000385450, 0.000579324 # -7 # 8.23 10 , 0.000258325 # -7 # 5.310112349832252e-10, 8.60 10 #f=log(1+x) # 0.019170747, 0.039720771 # 0.000300583, 0.000459759 # 0.000006061, 0.000210578 # 1.359261461164607e-7, 0.000006468 #Gauss did better all around!