I am trying to integrate with sympy but double checking with numpy. the correct answer for this integration is zero for n=1, which the numpy version produces. But the sympy version, which should have the same answer, doesn't. can someone tell me what's wrong with the sympy version?
import numpy as np
import sympy as sp
#*******************NUMPY ATTEMPT********************************************
T = 2*np.pi
n = 1 #integer
F = lambda t: t/(2*np.pi)
wT = 2*np.pi/T #frequency
def F_An(t):
return F(t)*np.cos(n*wT*t)
#F = lambda t: (t/(2*np.pi))*np.cos(n*wT*t)
An = quad(F_An,0,T)[0]
An = (2/T)*An
print('An numpy:', round(An,3))
#*******************SYMPY ATTEMPT********************************************
T = 2*sp.pi
n = sp.Symbol('n',integer=True,positive=True)
t = sp.symbols('t')
F = t/(2*sp.pi)
wT = 2*sp.pi/T #frequency
F_An=F*sp.cos(n*wT*t)
An = sp.integrate(F_An,t)
An = (2/T)*An
An=An.subs(t,T)
An=An.subs(n,1)
print('An sympy:', An)
with output:
An numpy: 0.0
An sympy: 1/(2*pi**2)
You haven't given limits in your call to integrate and are not using both limits. In quad you integrate from 0 to 2*pi
In [17]: F_An.integrate(t)
Out[17]:
t⋅sin(n⋅t) cos(n⋅t)
────────── + ────────
n 2
n
─────────────────────
2⋅π
In [18]: F_An.integrate(t).subs(t, 2*pi)
Out[18]:
1
──────
2
2⋅π⋅n
In [19]: F_An.integrate(t).subs(t, 2*pi) - F_An.integrate(t).subs(t, 0)
Out[19]: 0
In [20]: F_An.integrate((t, 0, 2*pi))
Out[20]: 0