整合平方函数

Integrate square function

I need to make the fourier series of an square function.

要得到这个,有一个函数 y = square(t)

我需要对 (2/p)*(square(t)*cos((2pi/p)*t)) 从 0 到我键入的变量进行积分,例如 p = 10 但我不能做这个积分,因为每次我尝试用 scipy 做这个时,我都会得到平方函数的错误。

from scipy.signal import *
from scipy.integrate import quad
from numpy import *
p = 10
na = arange(0,10,1)
def integrand(t, a, b):
    return square(t)*cos(b*((2*pi)/a)*t)
i,err = quad(integrand,0,p, args=(p,na))
y = i

quadpack.error: Supplied function does not return a valid float.

quad returns 2个参数,积分和误差的绝对值,解包即可。

from scipy.signal import *
from scipy.integrate import quad
from numpy import *
p = 10
def integrand(t, a):
    return square(t)*cos(((2*pi)/a)*t)
i, err = quad(integrand,0,p, args=(p,))
y = (2/p)*i

更新:

from scipy.signal import *
from scipy.integrate import quad
from numpy import *
p = 10
na = arange(0,10,1)
y = []

def integrand(t, a, b):
    return square(t)*cos(b*((2*pi)/a)*t)

for e in na:
    i,err = quad(integrand,0,p, args=(p, e))
    y.append(i)

print(y)