Python测试三角函数的正交性

import numpy as np
def Gaussian_Integral(a,b,f):#定义一元函数的7点高斯积分区间在-1 到 1上
    x_y_w=np.array([[-0.9491079123427585,0.1294849661688697],
                    [-0.7415311855993945,0.2797053914892766],
                    [-0.4058451513773972,0.3818300505051189],
                    [ 0.0000000000000000,0.4179591836734694],
                    [ 0.4058451513773972,0.3818300505051189],
                    [ 0.7415311855993945,0.2797053914892766],
                    [ 0.9491079123427585,0.1294849661688697]])
    S=0
    for i in range(0,np.size(x_y_w,0)):
         xq = x_y_w[i,0]
         wq = x_y_w[i,1]
         S=S+wq*f(((b-a)/2)*xq+(b+a)/2)
    #print(S*(b-a)/2)
    return S*(b-a)/2

from scipy.integrate import quad,dblquad,nquad
#
j=4
f=lambda x:np.cos(x*4)*np.cos(j*x)#/(2*np.pi)
I=Gaussian_Integral(-np.pi,np.pi,f)
I1=quad(f,-np.pi,np.pi)
print(I)
print(I1)
#3.917212793717355
#(3.141592653589793, 2.30587916072457e-09)

你可能感兴趣的:(Python,python,numpy)