Sie sind auf Seite 1von 10

Fall Semester 2019-20. Solution of Digital Lab Assignment-2 (MAT 2002).

Registration Number: 18BME0743 Name of Student: RAJAT MISHRA

Problem1: Input a function 𝑓(𝑥) which is periodic in interval (a, b). Find
fourier series for 𝑓(𝑥) up to 𝑛 harmonics and hence plot f(x) for its
Fourier series.
i) 𝑓(𝑥) = 𝑥2, 𝑎 = −𝜋, 𝑏 = 𝜋. (up to 3 harmonics)
MATLAB CODE:
syms x
f =input('Enter the function of x: ');
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);
Fx=a0/2;
for n=1:m
figure;
an(n)=(1/L)*int(f*cos(n*pi*x/L),a,b);
bn(n)=(1/L)*int(f*sin(n*pi*x/L),a,b);
Fx=Fx+an(n)*cos(n*pi*x/L)+bn(n)*sin(n*pi*x/L);
Fx=vpa(Fx,4);
ezplot(Fx,[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(Fx)))

MATLAB OUTPUT:
Enter the function of x: x^2
Enter the interval of [a,b]: [-pi,pi]
Enter the number of Harmonics required: 3
Fourier series with3harmonics is:1.0*cos(2.0*x) -
0.4444*cos(3.0*x) - 4.0*cos(x) + 3.29
(ii) f(x)= 1+2x/n;-pi<x<0 (up to 3 harmonics)
1-2x/n;0<x<pi

MATLAB CODE:

syms x
f =input('Enter the function of x: ');
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);
Fx=a0/2;
for n=1:m
figure;
an(n)=(1/L)*int(f*cos(n*pi*x/L),a,b);
bn(n)=(1/L)*int(f*sin(n*pi*x/L),a,b);
Fx=Fx+an(n)*cos(n*pi*x/L)+bn(n)*sin(n*pi*x/L);
Fx=vpa(Fx,4);
ezplot(Fx,[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(Fx)))

MATLAB OUTPUT:
Enter the function of x: (1+2*x/pi)*(heaviside(x+pi)-
heaviside(x))+(1-2*x/pi)*(heaviside(x)-heaviside(x-pi))
Enter the interval of [a,b]: [-pi,pi]
Enter the number of Harmonics required: 3
Fourier series with3harmonics is:0.09006*cos(3.0*x) +
0.8106*cos(x)
Problem 2: Write a code to obtain a fourier series for given set of
n data points. Run the code for the following data (for 4 harmonics):

MATLAB CODE:

Clc
Clear all
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)));
MATLAB OUTPUT:
Enter the equally spaced values of x: 0:6
Enter the values of y=f(x): [6 15 18 22 17 12 6]
Enter the number of harmonics required: 4
Fourier series with4harmonics is:1.168*sin(2.693*t) -
0.8268*cos(2.693*t) - 6.491*cos(0.8976*t) - 0.396*cos(1.795*t) -
0.8268*cos(3.59*t) + 4.302*sin(0.8976*t) + 0.6463*sin(1.795*t) -
1.168*sin(3.59*t) + 13.71
MATLAB CODE:

Clc
Clear all
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)));

MATLAB OUTPUT:
Enter the equally spaced values of x: [0:30:180]
Enter the values of y=f(x): [0 5224 8097 7850 5499 2626 0]
Enter the number of harmonics required: 4
Fourier series with4harmonics is:80.36*sin(0.08976*t) -
283.3*cos(0.08976*t) - 3187.0*cos(0.02992*t) -
714.4*cos(0.05984*t) - 283.3*cos(0.1197*t) +
2982.0*sin(0.02992*t) + 251.8*sin(0.05984*t) -
80.36*sin(0.1197*t) + 4185.0

Das könnte Ihnen auch gefallen