In [1]:
import sympy as s
s.init_printing()
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
s.var('x,F0,T,w',real=True,positive=True)
i = s.var('i',integer=True,positive=True)
x1 = T/10
x2 = 2*T/5
x3 = T/2
x4 = T
y1 = F0
m1 = y1/x1
m2 = -m1*x1/(x3-x2)
#w = 2*s.pi*i/T
f = s.Piecewise(
(m1*x,x<x1),
(y1,x<x2),
(m2*(x-x3),x<x3),
(0,x<=x4))
In [5]:
(2/T * s.integrate(f*s.cos(w*x),(x,0,x1))).expand()
Out[5]:
In [3]:
f
Out[3]:
In [11]:
2/T * s.integrate(f*s.sin(w*x),(x,0,x4))
Out[11]:
In [10]:
2/T * s.integrate(f,(x,0,x4))
Out[10]:
In [ ]:
In [47]:
print("$a_i = "+s.latex(ai)+"$")
In [48]:
print("$b_i = "+s.latex(bi)+"$")
In [49]:
print("$a_0 = "+s.latex(a0)+"$")
In [52]:
fs = s.fourier_series(f,(x,0,x4))
Out[52]:
In [82]:
fsn = fs.truncate(n=5)
ff = s.lambdify(x,fsn)
In [83]:
fsn
Out[83]:
In [84]:
xx = np.linspace(0,x4,1001)
yy = np.zeros_like(xx)
for i,xi in enumerate(xx):
yy[i] = ff(xi)
plt.plot(xx,yy)
Out[84]: