0% found this document useful (0 votes)
47 views8 pages

Application of Differential and Difference Equation

This document contains code for calculating the Fourier series approximation of functions based on their values over an interval. The code takes in the function, interval, and number of harmonics as input. It then calculates the Fourier coefficients and plots the Fourier approximation against the original function for the specified number of harmonics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views8 pages

Application of Differential and Difference Equation

This document contains code for calculating the Fourier series approximation of functions based on their values over an interval. The code takes in the function, interval, and number of harmonics as input. It then calculates the Fourier coefficients and plots the Fourier approximation against the original function for the specified number of harmonics.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

APPLICATION OF DIFFERENTIAL

AND DIFFERENCE EQUATION

NAME: DEBMALYA CHATTERJEE

REG.NO: 18BEC0132

SLOT: L7+L8

PROF.RAJASEKARAN G

DIGITAL ASSIGNMENT 2
ANSWER:

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)

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

Enter the number of Harmonics required: 4

OUTPUT:
Fourier series with4harmonics is:0.06354*cos(2.0*x) +
0.01869*cos(4.0*x) + 0.1271*sin(2.0*x) + 0.07476*sin(4.0*x)
+ 0.03177*cos(3.0*x) + 0.09531*sin(3.0*x) + 0.1589*cos(x) +
0.1589*sin(x) + 0.1589

SCREENSHOTS:
ANSWER:

CODE:
clear all
clc
syms t
T0=input('Enter the equally spaced values of x: ');
A=input('Enter the values of y=f(x): ');
m=input('Enter the number of harmonics required: ');
n=length(T0);a=T0(1);b=T0(n);
h=T0(2)-T0(1);
L=(b-a+h)/2;
theta=pi*T0/L;
a0=(2/n)*sum(A);
Fx=a0/2; x1=linspace(a,b,100);
for i=1:m
figure
an=(2/n)*sum(A.*cos(i*theta));
bn=(2/n)*sum(A.*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(T0,A);
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 T0: [0,1/6,1/3,1/2,2/3,5/6,1]

Enter the values of A=f(T0): [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

You might also like