Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Satvarsh Gondala

18BCE2098
Matlab DA 2

EXP 2A

1. Find the Fourier series expansion and diagram for the function
f(x) = x sin (x), in 0 < x < 2π.

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

Fourier series with3harmonics is:0.6667*cos(2.0*x) + 0.25*cos(3.0*x) - 0.5*cos(x) + 3.142*sin(x) -


1.0
>>
2. Find the Fourier series expansion and diagram for the function

(-pi)*(heaviside(x+pi)-heaviside(x))+x*(heaviside(x)-heaviside(x-pi))

>> untitled4
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: 3
Fourier series with3harmonics is:1.0*sin(3.0*x) - 0.07074*cos(3.0*x) - 0.5*sin(2.0*x) - 0.6366*cos(x)
+ 3.0*sin(x) - 0.7854
3. Find the Fourier series expansion and diagram for the function
f(x) = |x|, in −1 < x < 1.

>>Enter the function of x: abs(x)


>>Enter the interval of [a,b]: [-1,1]
>>Enter the number of Harmonics required: 2
>>Fourier series with2harmonics is:0.5 – 0.4053*cos(3.142*x)

4. Find the Fourier series expansion and diagram for the function
f(x) = e −x , in −π < x < π.
Enter the function of x: exp(-x)
Enter the interval of [a,b]: [-pi,pi]
Enter the number of Harmonics required: 2
Fourier series with2harmonics is:1.47*cos(2.0*x) + 2.941*sin(2.0*x) - 3.676*cos(x) - 3.676*sin(x) +
3.676

5. Find the Fourier series expansion and diagram for the function

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
Fourier series with3harmonics is:0.8488*sin(4.712*x) + 2.546*sin(1.571*x) + 2.0
EXP 2B

1)
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)));
1)Enter the equally spaced values of x: [0:30:330]
Enter the values of y=f(x): [1.8,1.1,0.3,0.16,1.5,1.3,2.16,1.25,1.3,1.52,1.76,2]
Enter the number of harmonics required: 3
Fourier series with3harmonics is:0.4458*cos(0.03491*t) - 0.04228*cos(0.01745*t) -
0.4794*sin(0.01745*t) - 0.3767*sin(0.03491*t) + 0.06333*cos(0.05236*t) + 0.085*sin(0.05236*t) +
1.346
2)
Enter the equally spaced values of x: [0:pi/6:5*pi/6]
Enter the values of y=f(x): [0,9.2,14.4,17.8,17.3,11.7]
Enter the number of harmonics required: 3
Fourier series with3harmonics is:0.1155*sin(4.0*t) - 2.833*cos(4.0*t) - 1.559*sin(2.0*t) -
7.733*cos(2.0*t) - 2.333*cos(6.0*t) + 6.469e-16*sin(6.0*t) + 11.73
>>
3)

Enter the equally spaced values of x: [0:60:300]


Enter the values of y=f(x): [9,18,24,28,26,20]
Enter the number of harmonics required: 3
Fourier series with3harmonics is:2.368e-15*sin(0.03491*t) - 2.333*cos(0.03491*t) -
1.155*sin(0.01745*t) - 8.333*cos(0.01745*t) - 2.333*cos(0.05236*t) + 2.041e-15*sin(0.05236*t) +
20.83
4)
Enter the equally spaced values of x: [30:30:360]
Enter the values of y=f(x): [2.16,1.25,1.3,1.52,1.76,2,1.25,1.3,1.52,1.76,2,1.2]
Enter the number of harmonics required: 2
Fourier series with2harmonics is:0.04849*cos(0.01745*t) + 0.175*cos(0.03491*t) -
0.02269*sin(0.01745*t) - 0.1559*sin(0.03491*t) + 1.585

You might also like