# HW 8 # Ha LUU # 2/21/2014 ############################################################################ #Problem 1 #ApplyNCg(f,x,n,a,b): inputs an expression f in the variable x, #and a positive integer n, 2 variable a,b as interval, applies the Newton-Cotes formula #found by FindNC(n) to approximate the integral of f(x) from a to b ApplyNCg:=proc(f,x,n,a,b) local L,i: L:=FindNC(n): (b-a)*(add(L[i]*subs(x=(i-1)*(b-a)/n+a,f),i=1..n+1)): end: ############################## #Problem 2 ApplyNCgg:= proc(f,x,n,a,b,K) local L,i,m,j: for j from 0 to K do m[j]:=a+(j)*(b-a)/K : #print(m[j]): od: for i from 0 to K-1 do L[i]:=(ApplyNCg(f,x,n,m[i],m[i+1])): #print(L[i]): od: add(L[i],i=0..K-1): end: ################# #Problem 3 ################################################### #BestNC(1/(1+x^2), x, 32) : is the the pair [2,16] #BestNC(1/(1+x^4),x, 32): is the pair [8,4] #BestNC(exp(x), x,32) : is the pair [4,8] #BestNC(log(1+x),x, 32) : is the pair[4,8] ################################################### ## Findfactor Findfactor:=proc(M) local n,i,j,k,L: j:=1: for i from 1 to M do if type(M/i,`integer`)=true then L[j][1]:=i: L[j][2]:=M/i: j:=j+1: fi: od: k:=j-1: [seq([seq(L[j][k],k=1..2)],j=1..k)]: end: ## BestNC(f,x,M) BestNC:=proc(f,x,M) local L,K,i,j,errval: L:= Findfactor(M): errval:=1: j=1: for i from 1 to nops(L) do K[i]:= abs(evalf(ApplyNCgg(f,x,L[i][1],0,1,L[i][2]) - int(f,x=0..1))): if K[i]