PI = 2.*np.arcsin(1) a=0 b= 2*PI N= 10 h = (a+b) / (N-1) x = np.zeros(N) y1 = np.zeros(N) y2 = np.zeros(N) y8 = np.zeros(N) for i in range(N): x[i] = a + h*i y1[i] = np.sin(x[i]) y2[i] = np.sin(2*x[i]) y8[i] = np.sin(8*x[i]) print(intr.trap_d(x,intr.multiply(y1,y1))) print(intr.trap_d(x,intr.multiply(y1,y2))) print(intr.trap_d(x,intr.multiply(y2,y8))) print(intr.trap_d(x,intr.multiply(y8,y8))) #2nd problem a2= 0 b2 = 1 N = [10, 100, 1000] N2 = len(N) for i in range(N2): x = np.zeros(N[i]) y = np.zeros(N[i]) print("for N = " + str(N[i])) print(intr.trap(intr.lin,a2,b2,N[i])) print(intr.trap(intr.quad,a2,b2,N[i]))
PI = 2. * np.arcsin(1) N = 1000 a = 0 b = 2. * PI h = (b - a) / (N - 1) x = np.zeros(N) y1 = np.zeros(N) y2 = np.zeros(N) y3 = np.zeros(N) for i in range(N): x[i] = a + h * i y1[i] = np.sin(x[i]) y2[i] = np.sin(2. * x[i]) y3[i] = np.sin(8. * x[i]) print(intr.trap_d(x,intr.multiply(y1,y1))) print(intr.trap_d(x,intr.multiply(y1,y2))) print(intr.trap_d(x,intr.multiply(y2,y3))) print(intr.trap_d(x,intr.multiply(y3,y3))) #We find that when m=n the scalar product is pi and when m=/n the scalar product #is 0. This is very similar to the scalar product of the legendre functions. def powr(x,n): m = 1 if n == 0: return 1 else: for l in range(n): m = m * x return m