MAT 2002-Applications of Differential and Difference Equations.
NAME: B. VIGNESHWAR REDDY REG.NO: 21MID0035
Lab Digital Assignment - 1
1-A
Aim: To find the Fourier series of the given function.
MATLAB CODE:
clear all
close all
clc
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)))
INPUT:
Enter the function of x:
(-1)*(heaviside(x+pi)-heaviside(x))+(1)*(heaviside(x)-heaviside(x-pi))
Enter the interval of [a,b]:
[-2,2]
Enter the number of Harmonics required:
3
OUTPUT:
Fourier series with3harmonicsis:0.4244*sin(4.712*x) + 1.273*sin(1.571*x)
SCREENSHOT:
Aim: To find the Fourier series of the given function.
MATLAB CODE:
clear all
close all
clc
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)))
INPUT:
Enter the function of x:
(0)*(heaviside(x+pi)-heaviside(x))+(4)*(heaviside(x)-heaviside(x-pi))
Enter the interval of [a,b]:
[-2,2]
Enter the number of Harmonics required:
3
OUTPUT:
Fourier series with3harmonics is:0.8488*sin(4.712*x) + 2.546*sin(1.571*x) + 2.0
SCREENSHOT:
Aim: To find the Fourier series of the given function.
MATLAB CODE:
clear all
close all
clc
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)))
INPUT:
Enter the function of x:
(exp(-x))*(heaviside(x)-heaviside(x-2*pi))
Enter the interval of [a,b]:
[0,2*pi]
Enter the number of Harmonics required:
3
OUTPUT:
Fourier series with3harmonics is:0.06354*cos(2.0*x) + 0.1271*sin(2.0*x) +
0.03177*cos(3.0*x) + 0.09531*sin(3.0*x) + 0.1589*cos(x) + 0.1589*sin(x) + 0.1589
SCREENSHOT:
Aim: To find the Fourier series of the given resulting periodic function.
MATLAB CODE:
clear all
close all
clc
syms t
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*t/L),a,b);
bn(n)=(1/L)*int(f*sin(n*pi*t/L),a,b);
Fx=Fx+an(n)*cos(n*pi*t/L)+bn(n)*sin(n*pi*t/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)))
INPUT:
Enter the function of t:
(0)*(heaviside(t+1/2)-heaviside(t))+(5*sin(2*pi)*t)*(heaviside(t)- heaviside(t-1/2))
Enter the interval of [a,b]:
[-1/2,+1/2]
Enter the number of Harmonics required:
3
OUTPUT:
Fourier series with3harmonics is:1.379e-17*cos(18.85*t) - 6.497e-17*sin(18.85*t) + 1.241e-
16*cos(6.283*t) - 1.949e-16*sin(6.283*t) + 9.745e-17*sin(12.57*t) - 1.531e-16
SCREENSHOT:
1-B
Aim: To show there is a direct current part of 0.75 amp in the variable current and to
obtain the amplitude of the first three harmonics.
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);
disp('Direct current');
disp(Fx);
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]
Enter the values of y=f(x):
[1.98 1.30 1.05 1.30 -0.88 -0.25]
Enter the number of harmonics required:
3
OUTPUT:
Direct current
0.7500
Fourier series with3harmonics is:2.192e-16*sin(18.85*t) - 0.06667*cos(18.85*t) +
0.3733*cos(6.283*t) + 0.89*cos(12.57*t) + 1.005*sin(6.283*t) - 0.1097*sin(12.57*t) + 0.75
Hence the direct current part of 0.75 amp in the variable current in shown.
SCREENSHOT:
Aim: To find the constant, the first sine and cosine terms in the Fourier
series expansion of the given function.
MATLAB CODE:
clear all
close all
clc
syms t
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*t/L),a,b);
bn(n)=(1/L)*int(f*sin(n*pi*t/L),a,b);
Fx=Fx+an(n)*cos(n*pi*t/L)+bn(n)*sin(n*pi*t/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)))
INPUT:
Enter the equally spaced values of x:
0:5
Enter the values of y=f(x):
[6 15 18 22 17 12]
Enter the number of harmonics required:
1
OUTPUT:
Fourier series with1harmonics is:1.155*sin(1.047*t) - 6.667*cos(1.047*t) + 15.0
SCREENSHOT:
Conclusion:
Hence, the Fourier series and the harmonic analysis of given functions are verified by using
Matlab.