Integrate square function

Question:

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

To get this a have a function y = square(t)

i need to make the integral of (2/p)*(square(t)*cos((2pi/p)*t)) from 0 to a variable I type for example p = 10 but i cant make this integral because every time i try to do this with scipy i get an error with the square function.

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.

Asked By: Daniel Daza

||

Answers:

quad returns 2 parameters, the integral and the absolute value of the error, you just have to unpack it.

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

update:

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)
Answered By: eyllanesc