Experiment 1-A Fourier Series: Course Code: MAT2002 Course Name: Application of Differential and Difference Equations
Experiment 1-A Fourier Series: Course Code: MAT2002 Course Name: Application of Differential and Difference Equations
Experiment 1–A
Fourier Series
AIM
EXAMPLE 1:
EXPERIMENT 1B
AIM: To compute and visualize the harmonics of a function from the given
data.
MATLAB:
clc
clear all
syms t
x=input('enter the equally spaced value of x');
y=input('enter the value of y=f(x) : ');
m=input('enter the number of harmonics reqd :');
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
subplot(2,4,i)
an=(2/n)*sum(y.*cos(i*theta));
bn=(2/n)*sum(y.*sin(i*theta));
disp(strcat('a',num2str(i),'=',num2str(an)))
disp(strcat('b',num2str(i),'=',num2str(bn)))
Fx=Fx+an*cos(i*pi*t/L)+bn*sin(i*pi*t/L);
Fx=vpa(Fx,m);
Fx1=subs(Fx,t,x1);
plot(x1,Fx1);
hold on
plot(x,y);
title(['fourier series with ',num2str(i),'harmonics'])
hold off;
end
disp(strcat('fourier series with ',num2str(i),'harmonicsis:',char(Fx)));
OUTPUT:
EXAMPLE 1:
enter the equally spaced value of x[0 1 2 3 4 5]
a1=-2.8333
b1=4.3301
a2=-1.5
b2=-0.86603
a3=2.6667
b3=-6.1232e-16
a4=-1.5
b4=0.86603
a5=-2.8333
b5=-4.3301
a6=14
b6=-7.5928e-15
a7=-2.8333
b7=4.3301
a8=-1.5
b8=-0.86603
GRAPH: