0% found this document useful (0 votes)
156 views

Experiment 1-A Fourier Series: Course Code: MAT2002 Course Name: Application of Differential and Difference Equations

This MATLAB code computes the Fourier series representation of functions from sampled data points. It takes in equally spaced x-values and corresponding y-values, computes the Fourier coefficients up to a specified number of harmonics, and plots the Fourier series approximation against the original function values. Examples are provided where Fourier series for functions like x*sin(x), 2*x-x^2, and piecewise definitions are computed up to 2-8 harmonics and visualized.

Uploaded by

ashish
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
156 views

Experiment 1-A Fourier Series: Course Code: MAT2002 Course Name: Application of Differential and Difference Equations

This MATLAB code computes the Fourier series representation of functions from sampled data points. It takes in equally spaced x-values and corresponding y-values, computes the Fourier coefficients up to a specified number of harmonics, and plots the Fourier series approximation against the original function values. Examples are provided where Fourier series for functions like x*sin(x), 2*x-x^2, and piecewise definitions are computed up to 2-8 harmonics and visualized.

Uploaded by

ashish
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Course Code: MAT2002

Course Name: Application of Differential and Difference Equations

Experiment 1–A
Fourier Series

AIM

Fourier series for continous functions and harmonics analysis.


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)))

EXAMPLE 1:

Enter the function of x: x*sin(x)

Enter the interval of [a,b]: [0,2*pi]

Enter the number of Harmonics required: 2


Fourier series with2harmonics is:0.6667*cos(2.0*x) - 0.5*cos(x) + 3.142*sin(x) - 1.0
EXAMPLE 2:

Enter the function of x: 2*x-x^2

Enter the interval of [a,b]: [0,3]

Enter the number of Harmonics required: 4

Fourier series with4harmonics is:0.9549*sin(2.094*x) - 0.228*cos(4.189*x) - 0.05699*cos(8.378*x) -


0.9119*cos(2.094*x) + 0.4775*sin(4.189*x) + 0.2387*sin(8.378*x) - 0.1013*cos(6.283*x) +
0.3183*sin(6.283*x)
EXAMPLE 3:

Enter the function of x: -pi*(heaviside(x+pi)-heaviside(x))+x*(heaviside(x)-heaviside(x-pi))

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

Enter the number of Harmonics required: 2


Fourier series with2harmonics is:3.0*sin(x) - 0.6366*cos(x) - 0.5*sin(2.0*x) - 0.7854

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]

enter the value of y=f(x) : [4 8 15 7 6 2]

enter the number of harmonics reqd :8

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

fourier series with8harmonicsis:4.330127*sin(1.0471976*t) - 1.5*cos(2.0943951*t) -


1.5*cos(4.1887902*t) - 1.5*cos(8.3775804*t) - 2.8333333*cos(1.0471976*t) -
0.8660254*sin(2.0943951*t) + 0.8660254*sin(4.1887902*t) - 0.8660254*sin(8.3775804*t) -
2.8333333*cos(5.2359878*t) - 4.330127*sin(5.2359878*t) + 2.6666667*cos(3.1415927*t) +
14.0*cos(6.2831853*t) - 6.123234e-16*sin(3.1415927*t) - 7.5928102e-15*sin(6.2831853*t) -
2.8333333*cos(7.3303829*t) + 4.330127*sin(7.3303829*t) + 7.0

GRAPH:

You might also like