0% found this document useful (0 votes)
39 views13 pages

Word Taken

The document contains MATLAB code to perform Fourier series analysis on functions. It includes code to: 1) Calculate the Fourier series of the function exp(-t) on the interval [-pi, pi] with 3 harmonics. 2) Calculate the Fourier series of the function t+t^2 on the interval [-pi, pi] with 2 harmonics. 3) Correct code to calculate a Fourier series with specified harmonics.

Uploaded by

safal
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)
39 views13 pages

Word Taken

The document contains MATLAB code to perform Fourier series analysis on functions. It includes code to: 1) Calculate the Fourier series of the function exp(-t) on the interval [-pi, pi] with 3 harmonics. 2) Calculate the Fourier series of the function t+t^2 on the interval [-pi, pi] with 2 harmonics. 3) Correct code to calculate a Fourier series with specified harmonics.

Uploaded by

safal
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/ 13

AIM: Question1)

MATLAB Code:

a)

clear all
close all
clc
syms t
f = input('Enter the function of t : ');
I=input('Enter the interval of [a,b]: ');
m=input('Enter the number of Harmonics required: ');
a=I(1);b=I(2);
L=(b-a)/2;
a0=(1/L)*int(f,a,b);
Ft=a0/2;
for n=1:m
figure;
an(n)=(1/L)*int(f*cos(n*pi*t/L),a,b);
bn(n)=(1/L)*int(f*sin(n*pi*t/L),a,b);
Ft=Ft+an(n)*cos(n*pi*t/L)+bn(n)*sin(n*pi*t/L);
Ft=vpa(Ft,4);
ezplot(Ft,[a,b]);
hold on ezplot(f,
[a,b]);
title(['Fourier Series with',num2str(n),'harmonics']);
legend('Fourier Series','Function Plot');
hold off
end
disp(strcat('Fourier series with',num2str(n),'harmonics is : ',char(Ft)))

INPUT
Enter the function of t : exp(-t)

Enter the interval of [a,b]: [-pi,pi]

Enter the number of Harmonics required: 3

OUTPUT

Fourier series with3harmonics is :1.47*cos(2.0*t) + 2.941*sin(2.0*t) -

0.7352*cos(3.0*t) - 2.206*sin(3.0*t) - 3.676*cos(t) - 3.676*sin(t) +

3.676

GRAPHS
b)
MATLAB Code:
clear all
close all
clc
syms t
f = input('Enter the function of t : ');
I=input('Enter the interval of [a,b]: ');
m=input('Enter the number of Harmonics required: ');
a=I(1);b=I(2);
L=(b-a)/2;
a0=(1/L)*int(f,a,b);
Ft=a0/2;
for n=1:m
figure;
an(n)=(1/L)*int(f*cos(n*pi*t/L),a,b);
bn(n)=(1/L)*int(f*sin(n*pi*t/L),a,b);
Ft=Ft+an(n)*cos(n*pi*t/L)+bn(n)*sin(n*pi*t/L);
Ft=vpa(Ft,4);
ezplot(Ft,[a,b]);
hold on ezplot(f,
[a,b]);
title(['Fourier Series with',num2str(n),'harmonics']);
legend('Fourier Series','Function Plot');
hold off
end
disp(strcat('Fourier series with',num2str(n),'harmonics is : ',char(Ft)))
INPUT

Enter the function of t : t+t^2


Enter the interval of [a,b]: [-
pi,pi]
Enter the number of Harmonics required: 2

OUTPUT

Fourier series with2harmonics is :1.0*cos(2.0*t) - 1.0*sin(2.0*t) -

4.0*cos(t) + 2.0*sin(t) + 3.29

GRAPHS:
Question 2

CORRECTED CODE

syms x
l=5;
N=5;
p=0;
for n=1:2:2*N+1 p=p+
(l/n*2)*sin(n*pi/2)*cos(n*pi*x/l)*sin(n*pi*x/l);
end
y=(4*l/pi^2)*p
OUTPUT
P=
2*cos(pi*x)*sin(pi*x) + 10*cos((pi*x)/5)*sin((pi*x)/5) -
(10*cos((3*pi*x)/5)*sin((3*pi*x)/5))/3 -
(10*cos((7*pi*x)/5)*sin((7*pi*x)/5))/7 +
(10*cos((9*pi*x)/5)*sin((9*pi*x)/5))/9 -
(10*cos((11*pi*x)/5)*sin((11*pi*x)/5))/11

Question 3

MATLAB Code:
clear all
clc
syms t
x=input('Enter the equally spaced values of x: ');
y=input('Enter the values of y=f(x): ');
m=input('Enter the number of harmonics required: ');
n=length(x);a=x(1);b=x(n);
h=x(2)-x(1);
L=(b-a+h)/2;
theta=pi*x/L;
a0=(2/n)*sum(y);
Fx=a0/2; x1=linspace(a,b,100);
for i=1:m
figure
an=(2/n)*sum(y.*cos(i*theta));
bn=(2/n)*sum(y.*sin(i*theta));
Fx=Fx+an*cos(i*pi*t/L)+bn*sin(i*pi*t/L) ;
Fx=vpa(Fx,4);
Fx1=subs(Fx,t,x1);
plot(x1,Fx1);
hold on
plot(x,y);
title(['Fourier Series with ',num2str( i ),'harmonics'])
legend('Fourier Series', 'Function Plot')
hold off;
end
disp(strcat('Fourier series with', num2str(i),'harmonics is:',char(Fx)));
INPUT

Enter the equally spaced values of x: [0 1/6 1/3 1/2 2/3 5/6 1]

Enter the values of y=f(x): [1.98 1.30 1.05 1.30 -0.88 -0.25 1.98]

Enter the number of harmonics required: 3

OUTPUT:Fourier series with3harmonics is:0.2325*sin(16.16*t) -

0.1628*cos(16.16*t) + 0.991*cos(5.386*t) + 0.2261*cos(10.77*t) +

0.4805*sin(5.386*t) - 0.8375*sin(10.77*t) + 0.9257

GRAPHS:
QUESTION 3b
OUTPUT
Fourier series with1harmonics is:1.155*sin(1.047*t) -
6.667*cos(1.047*t) + 15.0

You might also like