# HW 32 # # The integrate.quad function cannot handle # the improper integrals from Problems 1 and # 3 from HW 29. It can do the second problem. import numpy as np from scipy import integrate def hw29p2(x): return np.exp(-x**2) def hw30p1(x,r): return np.exp(-r*x) def hw30p2(x,m,n): return np.sin(m*x)*np.cos(n*x) def hw30p3(x,r): return np.exp(-r*x)*np.cos(2*x) # HW 29, P2 s1 = integrate.quad(hw29p2,0,np.inf) exact = np.sqrt(np.pi)/2 re = (s1[0]-exact)/exact print('HW 29, p2 =',s1,re) # Hw 30, P1 r = 0.3 s2 = integrate.quad(lambda x: hw30p1(x,r),0,2) print('HW 30, p1 = ',s2) # HW 30, P2 m = 0.5 n = 1.4 s3 = integrate.quad(lambda x: hw30p2(x,m,n),0,2) print('HW 30, p2',s3) # HW 30, P3 I = [] r = [] print('r I') for i in range(11): rval = i/10 r.append(rval) temp = integrate.quad(lambda x: hw30p3(x,rval),0,1) I.append(temp[0]) print(rval,temp[0]) # HW 29, p2 = (0.8862269254527579, 7.101318390915439e-09) 0.0 # HW 30, p1 = (1.5039612130199118, 1.6697323668378858e-14) # HW 30, p2 (-0.21047164888710243, 5.512197086338426e-15) # r I # 0.0 0.45464871341284085 # 0.1 0.44468508597054823 # 0.2 0.43492128134880575 # 0.3 0.4253630687826127 # 0.4 0.4160147145847368 # 0.5 0.4068791632915984 # 0.6 0.3979582001736366 # 0.7 0.3892525969149162 # 0.8 0.3807622420968074 # 0.9 0.37248625796640916 # 1.0 0.3644231048305502