0% found this document useful (0 votes)
46 views11 pages

Week 4 18bce118

The document discusses Fourier series and includes 6 examples of using MATLAB code to calculate the Fourier series for different periodic functions over different intervals. For each example, the code takes in the domain interval, partition, periodic function, and number of terms, runs the Fourier series calculation, and displays the result.

Uploaded by

Casinova
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views11 pages

Week 4 18bce118

The document discusses Fourier series and includes 6 examples of using MATLAB code to calculate the Fourier series for different periodic functions over different intervals. For each example, the code takes in the domain interval, partition, periodic function, and number of terms, runs the Fourier series calculation, and displays the result.

Uploaded by

Casinova
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Week 4:

Fourier series

INPUT:
clc
clear all
syms x
ll=input('enter the lower domain= ')
ul=input('enter the upper domain= ')
l=(ul-ll)/2
pr=input('enter the partition of [-l,l] or [0,2*l] as a vector: ')
f=input('enter the periodic function as a vector: ')
M=input('enter the no. of terms required in the series: ')
for n=1:M
for i=1:length(pr)-1
aa_0(i)=int(f(i),x,pr(i),pr(i+1));
aa(i)=int(f(i)*cos(n*pi*x/l),x,pr(i),pr(i+1));
bb(i)=int(f(i)*sin(n*pi*x/l),x,pr(i),pr(i+1));
end
a_0=vpa((1/l)*sum(aa_0),3);
a(n)=vpa((1/l)*sum(aa),3);
b(n)=vpa((1/l)*sum(bb),3);
F(n)=a(n)*cos(n*pi*x/l)+b(n)*sin(n*pi*x/l);
end
F_S=(a_0/2)+sum(F);
disp('the fourier series of the function f ')
disp(vpa(F_S,3))
h1=ezplot(F_S,[ll,ul]);
set(h1,'color','r')

Output:
enter the lower domain= 0

ll =0

enter the upper domain= 2*pi

ul = 6.2832

l = 3.1416

enter the partition of [-l,l] or [0,2*l] as a vector: [0 2*pi]

pr =0 6.2832

enter the periodic function as a vector: exp(-x)

f =exp(-x)

enter the no. of terms required in the series:


M =3

the fourier series of the function f

0.0635*cos(2.0*x) + 0.127*sin(2.0*x) + 0.0318*cos(3.0*x) + 0.0953*sin(3.0*x) + 0.159*cos(x) +


0.159*sin(x) + 0.159

2)

Output:

enter the lower domain= -pi

ll = -3.1416

enter the upper domain= pi

ul =3.1416

l = 3.1416

enter the partition of [-l,l] or [0,2*l] as a vector: [-pi pi]

pr =-3.1416 3.1416

enter the periodic function as a vector: x-x^2

f =- x^2 + x

enter the no. of terms required in the series: 5

M =5
the fourier series of the function f

0.16*cos(5.0*x) - 0.25*cos(4.0*x) - 1.0*sin(2.0*x) - 0.5*sin(4.0*x) - 1.0*cos(2.0*x) + 0.4*sin(5.0*x)


+ 0.444*cos(3.0*x) + 0.667*sin(3.0*x) + 4.0*cos(x) + 2.0*sin(x) - 3.29

3)

Output :

enter the lower domain= 0

ll =

enter the upper domain= 2*pi

ul =
6.2832

l=

3.1416

enter the partition of [-l,l] or [0,2*l] as a vector: [0 pi 2*pi]

pr =

0 3.1416 6.2832

enter the periodic function as a vector: [sin(x) 0]

f=

[ sin(x), 0]

enter the no. of terms required in the series: 5

M=

5
the fourier series of the function f

0.5*sin(x) - 0.0424*cos(4.0*x) - 0.212*cos(2.0*x) + 0.318

4) enter the lower domain= -pi

ll =

-3.1416

enter the upper domain= pi

ul =
3.1416

l=

3.1416

enter the partition of [-l,l] or [0,2*l] as a vector: [-pi 0 pi]

pr =

-3.1416 0 3.1416

enter the periodic function as a vector: [-pi x]

f=

[ -pi, x]

enter the no. of terms required in the series: 5

M=

5
the fourier series of the function f

0.6*sin(5.0*x) - 0.25*sin(4.0*x) - 0.0255*cos(5.0*x) - 0.5*sin(2.0*x) - 0.0707*cos(3.0*x) +


1.0*sin(3.0*x) - 0.637*cos(x) + 3.0*sin(x) - 0.785

5)

enter the lower domain= 0

ll =

enter the upper domain= 2

ul =
2

l=

enter the partition of [-l,l] or [0,2*l] as a vector: [0 2]

pr =

0 2

enter the periodic function as a vector: (1-x)^2

f=

(x - 1)^2

enter the no. of terms required in the series: 5

M=

5
the fourier series of the function f

0.045*cos(9.42*x) + 0.405*cos(3.14*x) + 0.101*cos(6.28*x) + 0.0253*cos(12.6*x) +


0.0162*cos(15.7*x) + 0.333

6)

enter the lower domain= 0

ll =

enter the upper domain= 2


ul =

l=

enter the partition of [-l,l] or [0,2*l] as a vector: [0 1 2]

pr =

0 1 2

enter the periodic function as a vector: [pi*x pi*(2-x)]

f=

[ pi*x, -pi*(x - 2)]

enter the no. of terms required in the series: 5

M=
5

the fourier series of the function f

1.57 - 1.27*cos(3.14*x) - 0.0509*cos(15.7*x) - 0.141*cos(9.42*x)

You might also like