You are on page 1of 15

9/3/23, 6:07 PM Tut 5 Fourier Series SageMath

Fourier Series
let f (x) be a periodic function of peroid 2L define in the interval (C , C + 2L) can be represnted in the
form fourier series as

$f(x) = \frac{a0}{2} + \sum{n=1}^{\infty} [


a_n\cos(\frac{n\pi x}{L}) +
b_n\sin(\frac{n\pi x}{L})]$<br>

1 C +2L
where a0 =
L

C
f (x)dx

1 C +2L nπx
an = ∫ f (x) cos( )dx
L C L

1 C +2L nπx
bn = ∫ f (x) sin( )dx
L C L

In [15]:

var('x')
var('n')
f(x) = x^2
assume(n,'integer')
f = piecewise([[[-1,1],x^2]])
L = 1
an=(1/L)*integrate((x^2)*cos(n*pi*x),x,-1,1)
a0=(1/L)*integrate(f,x,-1,1)
bn=(1/L)*integrate((x^2)*sin(n*pi*x),x,-1,1)
s =a0/2+sum(an*cos(n*pi*x/L)+bn*sin(n*pi*x/L),n,1,5)
show(an)
show(bn)
show(a0)
show(s)
n
4 (−1)

2 2
π n

144 cos(5 πx) − 225 cos(4 πx) + 400 cos(3 πx) − 900 cos(2 πx) + 3600 cos(πx)

2
900 π

localhost:8888/nbconvert/html/Downloads/Tut 5 Fourier Series SageMath.ipynb?download=false 1/15


9/3/23, 6:07 PM Tut 5 Fourier Series SageMath

In [16]:

plot(f,-1,1,legend_label="x^2") + plot(s,-1,1,color = "red",legend_label="fourier serie


s")

Out[16]:

In [17]:

g = f.fourier_series_partial_sum(10)
show(g)

cos(10 πx) 4 cos(9 πx) cos(8 πx) 4 cos(7 πx) cos(6 πx) 4 cos(5 πx)
− + − + −
2 2 2 2 2 2
25 π 81 π 16 π 49 π 9π 25 π

1
+
3

In [18]:

show(f.fourier_series_cosine_coefficient(5))

4

2
25 π

In [19]:

show(f.fourier_series_sine_coefficient(40))

localhost:8888/nbconvert/html/Downloads/Tut 5 Fourier Series SageMath.ipynb?download=false 2/15


9/3/23, 6:07 PM Tut 5 Fourier Series SageMath

In [20]:

var('x')
var('n')
f(x) = x^2
assume(n,'integer')
f = piecewise([[[-pi,pi],x^2]])
c = -pi
L = pi
an=(1/pi)*integrate((x^2)*cos(n*pi*x/L),x,-pi,pi)
a0=(1/pi)*integrate(x^2,x,-pi,pi)
bn=(1/pi)*integrate((x^2)*sin(n*pi*x/L),x,-pi,pi)
s =a0/2+sum(an*cos(n*pi*x/L)+bn*sin(n*pi*x/L),n,1,10)
show(an)
show(bn)
show(a0)
show(s)
n
4 (−1)

2
n

2 2
π
3

1 2
1 4 1 4 1
π + cos(10 x) − cos(9 x) + cos(8 x) − cos(7 x) + cos(6 x) −
3 25 81 16 49 9 2

(x)

In [21]:

plot(f,-pi,pi,legend_label="x^2") + plot(s,-pi,pi,color = "red",legend_label="fourier s


eries")

Out[21]:

localhost:8888/nbconvert/html/Downloads/Tut 5 Fourier Series SageMath.ipynb?download=false 3/15


9/3/23, 6:07 PM Tut 5 Fourier Series SageMath

In [ ]:

plot??

In [23]:

g = f.fourier_series_partial_sum(5)
show(f.fourier_series_partial_sum(5))

1 2
4 1 4
π − cos(5 x) + cos(4 x) − cos(3 x) + cos(2 x) − 4 cos(x)
3 25 4 9

In [24]:

show(f.fourier_series_cosine_coefficient(4))

In [25]:

show(f.fourier_series_sine_coefficient(4))

In [26]:

x = var('x')
f1(x) = x^2
f = piecewise([[(0,2),f1]])
g = f.fourier_series_partial_sum(15)
show(g)

4 sin(15 πx) 2 sin(14 πx) 4 sin(13 πx) sin(12 πx) 4 sin(11 πx) 2
− − − − − −
15 π 7π 13 π 3π 11 π

2 sin(6 πx) 4 sin(5 πx) sin(4 πx) 4 sin(3 πx) 2 sin(2 πx) 4 sin
− − − − − −
3π 5π π 3π π π

cos(12 πx) 4 cos(11 πx) cos(10 πx) 4 cos(9 πx) cos(8 πx) 4 c
+ + + + + +
2 2 2 2 2
36 π 121 π 25 π 81 π 16 π

4 cos(3 πx) cos(2 πx) 4 cos(


+ + +
2 2 2
9π π π

localhost:8888/nbconvert/html/Downloads/Tut 5 Fourier Series SageMath.ipynb?download=false 4/15


9/3/23, 6:07 PM Tut 5 Fourier Series SageMath

In [21]:

plot(g,0,2,color="red")+plot(f1,0,2,color="green")

Out[21]:

In [27]:

f = piecewise([((-1,0), -1), ((0,1), 1)])


g = f.fourier_series_partial_sum(20)
show(g)

4 sin(19 πx) 4 sin(17 πx) 4 sin(15 πx) 4 sin(13 πx) 4 sin(11 πx) 4
+ + + + +
19 π 17 π 15 π 13 π 11 π

4 sin(πx)
+
π

localhost:8888/nbconvert/html/Downloads/Tut 5 Fourier Series SageMath.ipynb?download=false 5/15


9/3/23, 6:07 PM Tut 5 Fourier Series SageMath

In [25]:

plot(f) + plot(g,-1,1,color ='red')

Out[25]:

localhost:8888/nbconvert/html/Downloads/Tut 5 Fourier Series SageMath.ipynb?download=false 6/15


9/3/23, 6:07 PM Tut 5 Fourier Series SageMath

In [28]:

f = piecewise([((-1,0), 0), ((0,1/2), 2*x),((1/2,1), 2*(1-x))])


g = f.fourier_series_partial_sum(15)
show(g)
plot(f)+plot(g,-1,1,color = "red")

2 cos(14 πx) 2 cos(10 πx) 2 cos(6 πx) 2 cos(2 πx) 4 sin(15 πx) 4
− − − − − +
2 2 2 2 2
49 π 25 π 9π π 225 π

4 sin(7 πx) 4 sin(5 πx) 4 sin(3 πx) 4 si


− + − +
2 2 2
49 π 25 π 9π

Out[28]:

In [31]:

f1(x) = -1
f2(x) = 2
f = piecewise([[(-pi,pi/2),f1],[(pi/2,pi),f2]])
g = f.fourier_series_partial_sum(25)
show(g)

3 cos(25 x) 3 cos(23 x) cos(21 x) 3 cos(19 x) 3 cos(17 x) co


− + − + − +
25 π 23 π 7π 19 π 17 π

3 cos(7 x) 3 cos(5 x) cos(3 x) 3 cos(x) 3 sin(25 x) 3 sin(23 x)


+ − + − + + −
7π 5π π π 25 π 23 π

3 sin(17 x) sin(15 x) 3 sin(14 x) 3 sin(13 x) 3 sin(11 x) 3 sin(10


+ + − + + −
17 π 5π 7π 13 π 11 π 5π

sin(3 x) 3 sin(2 x) 3 sin(x


+ − +
π π π

localhost:8888/nbconvert/html/Downloads/Tut 5 Fourier Series SageMath.ipynb?download=false 7/15


9/3/23, 6:07 PM Tut 5 Fourier Series SageMath

In [34]:

f = piecewise([((0,1), x), ((1,2), 2-x)])


g = f.fourier_series_partial_sum(15)
show(g)

4 cos(15 πx) 4 cos(13 πx) 4 cos(11 πx) 4 cos(9 πx) 4 cos(7 πx) 4
− − − − − −
2 2 2 2 2
225 π 169 π 121 π 81 π 49 π

In [35]:

var('x')
var('n')
f(x) = x/2
assume(n,'integer')
f = piecewise([[[0,1],x/2]])
c = 0
L = 1/2
an=(1/L)*integrate((x/2)*cos(2*n*pi*x),x,0,1)
a0=(1/L)*integrate(f,x,0,1)
bn=(1/L)*integrate((x/2)*sin(2*n*pi*x),x,0,1)
s =(a0/2)+sum(an*cos(n*pi*x/L)+bn*sin(n*pi*x/L),n,1,5)
show(an)
show(bn)
show(a0)
show(s)

1

2 πn

12 sin(10 πx) + 15 sin(8 πx) + 20 sin(6 πx) + 30 sin(4 πx) + 60 sin(2 πx) 1
− +
120 π 4

In [36]:

g = f.fourier_series_partial_sum(10)
show(g)

sin(20 πx) sin(18 πx) sin(16 πx) sin(14 πx) sin(12 πx) sin(10 πx
− − − − − −
20 π 18 π 16 π 14 π 12 π 10 π

1
+
4

localhost:8888/nbconvert/html/Downloads/Tut 5 Fourier Series SageMath.ipynb?download=false 8/15


9/3/23, 6:07 PM Tut 5 Fourier Series SageMath

In [31]:

plot(f,0,1) + plot(g,0,1,color = 'red')

Out[31]:

localhost:8888/nbconvert/html/Downloads/Tut 5 Fourier Series SageMath.ipynb?download=false 9/15


9/3/23, 6:07 PM Tut 5 Fourier Series SageMath

In [32]:

n = var("n")
frames = []
xr = (x, 0, 1)
for k in srange(1,5):
g = plot(f.fourier_series_partial_sum(k), xr, color="blue", legend_label='k = %d' %
k)
g += plot(x/2, xr, color="green", legend_label="x/2")
frames.append(g)

a = animate(frames, ymin=0.0, ymax=1.0, legend_loc=(0.2,0.8))


a.show()

In [37]:

animate??

The alternative form to the classical Fourier series is


its complex form Fourier series
∞ 1 C +2L
niπx/L −niπx/L
f (x) = ∑ Cn e where Cn = ∫ f (x) e dx
n=−∞ 2L C

localhost:8888/nbconvert/html/Downloads/Tut 5 Fourier Series SageMath.ipynb?download=false 10/15


9/3/23, 6:07 PM Tut 5 Fourier Series SageMath

In [38]:

var('x')
var('n')
assume(n,'integer')
L=1;
f=piecewise([[(0,2),(e)^(2*x)]])
Cn=1/2*integrate((e)^(2*x)*e^(-i*n*pi*x),x,0,2)
show(Cn)

cosh(4) + sinh(4) 1

2 (−i πn + 2) 2 (−i πn + 2)

In [39]:

s =sum(Cn*e^(i*n*pi*x),n,-1,1)
show(s)

(2i π − 2 (i π − 2) cosh(4) + (−2i π − 2 (−i π − 2) cosh(4) − 2 (−i π − 2) sinh(4)

2 2 2 (i πx)
− (π − (π + 4) cosh(4) − (π + 4) sinh(4) + 4)e − 2 (i π − 2) sinh(4) − 4)

2
4 (π + 4)

1) Find fourier Series of the follwoing function and also plot graph of function and fourier series.

i) f (x) = e
(−x)
in (0, 2π )

ii) f(x) = xsinx in (0, 2π )

2) obtain half range sine series in (0,π ) for cosx

3) obtain half range cosine series in (0,π ) for f(x) = x(π - x)

4) Find the complex form of the Fourier series for f(x) = 2x in (0,2π )

localhost:8888/nbconvert/html/Downloads/Tut 5 Fourier Series SageMath.ipynb?download=false 11/15


9/3/23, 6:07 PM Tut 5 Fourier Series SageMath

In [41]:

# Q1 i)
var('x')
var('n')
f(x)=exp(-x)
L=2*pi
an=(1/L)*integrate(f*cos(n*x),x,0,2*pi)
a0=(1/L)*integrate(f,x,0,2*pi)
bn=(1/L)*integrate(f*sin(n*x),x,0,2*pi)
s=a0/2+ sum(an*cos(n*x)+ bn*sin(n*x),n,1,5)
show("an=",an)
show("bn=",bn)
show("a0=",a0)
show("s=",s)
(−2 π)
e 1

2 2
n +1 n +1
an= −

(−2 π)
ne n

2 2
n +1 n +1
bn= −

(−2 π)
e − 1
a0= −

(2 π) (2 π) (2 π)
(85 (e − 1) cos(5 x) + 130 (e − 1) cos(4 x) + 221 (e − 1) cos(3 x) +

(2 π) (2 π) (2 π)
(e − 1) sin(5 x) + 520 (e − 1) sin(4 x) + 663 (e − 1) sin(3 x) + 884
s=
4420 π
(−2 π)
e − 1

localhost:8888/nbconvert/html/Downloads/Tut 5 Fourier Series SageMath.ipynb?download=false 12/15


9/3/23, 6:07 PM Tut 5 Fourier Series SageMath

In [43]:

p1 = plot(f,(x,0,2*pi), legend_label= 'f(x) = e^(-x)')


p2 = plot(s,(x,0,2*pi), linestyle='--', legend_label= 'Fourier Series (5 terms)')
show(p1+p2)

localhost:8888/nbconvert/html/Downloads/Tut 5 Fourier Series SageMath.ipynb?download=false 13/15


9/3/23, 6:07 PM Tut 5 Fourier Series SageMath

In [59]:

#Q1 ii)
x = var('x')
f(x) = x * sin(x)
p = plot(f(x), (x, 0, 2*pi), ymin=0, ymax=2*pi, legend_label='f(x) = x*sin(x)', axes_la
bels=['x', 'f(x)'])
show(p)

localhost:8888/nbconvert/html/Downloads/Tut 5 Fourier Series SageMath.ipynb?download=false 14/15


9/3/23, 6:07 PM Tut 5 Fourier Series SageMath

In [60]:

#Q2
x = var('x')

a_0 = (2 / pi) * integrate(cos(x), x, 0, pi)

n = var('n')
a_n = (2 / pi) * integrate(cos(x) * sin(n * x), x, 0, pi)

half_range_sine_series = a_0 / 2 + sum(a_n * sin(n * x) for n in range(1, 11))

show(half_range_sine_series)
n cos(πn) n n cos(πn) n n cos(πn) n
2( + ) sin(10 x) 2( + ) sin(9 x) 2( +
2 2 2 2 2 2
n −1 n −1 n −1 n −1 n −1 n
+ +
π π π

n cos(πn) n n cos(πn) n n cos(πn)


2( + ) sin(6 x) 2( + ) sin(5 x) 2( +
2 2 2 2 2 2
n −1 n −1 n −1 n −1 n −1 n
+ + +
π π π

n cos(πn) n cos(πn)
n
2( + ) sin(2 x) 2( +
2 2 2
n −1 n −1 n −1 n
+ +
π π

In [64]:

#Q3
var('x n')
f(x) = x*(pi - x)
L = pi
a0 = (1/L)* integrate(f, x, 0, pi)
an = [(1/L) * integrate(f* cos(n*x), x, 0, pi) for n in range(1, 6)]
half_range_cosine_series = a0/2 + sum(an[n-1] * cos(n*x) for n in range(1,6))
show(half_range_cosine_series)

1 2
1 1
π − cos(4 x) − cos(2 x)
12 8 2

In [75]:

#Q4
var('x n')
f(x) = 2*x
L = 2*pi

# Calculate the complex Fourier series

fourier_series_complex= sum((1/L)*integral(f * exp(-I * n*x), x, 0, 2*pi) * exp(I* n*x)


for n in range(-5,6))
show(fourier_series_complex)

2 (5i x)
1 (4i x)
2 (3i x) (2i x) (i x) (−i x) (−2i x)
2π + ie + ie + ie + ie + 2i e − 2i e − ie −
5 2 3

localhost:8888/nbconvert/html/Downloads/Tut 5 Fourier Series SageMath.ipynb?download=false 15/15

You might also like