Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

//EXPANSION AND GRAPHICAL REPESENTATION OF FIRST n TERMS OF FOURIER

SERIES
//NAME- MAYANK RANA
//ROLL NO-7665
funcprot(0);
clc
function dy=f(x)//Given function
dy(find(x<=%pi))=-1
dy(find(x>%pi))=1
endfunction
x=[0:.1:2*%pi]//range of the function
a0=1/%pi*intg(0,2*%pi,f,.0000001);//first term of fourier series
s=a0/2;
title("square wave fourier series expansion of first term")
subplot(2,2,1)
for i=1:1
function b=f1(x, f)
b=f(x)*cos(i*x);
endfunction
function c=f2(x, f)
c=f(x)*sin(i*x);
endfunction
A(i)=1/%pi*intg(0,2*%pi,f1,.0000001);//An terms of fourier series
B(i)=1/%pi*intg(0,2*%pi,f2,.0000001);//Bn terms of Fourier series
s=s+A(i)*cos(i*x)+B(i)*sin(i*x);//generating formula of fourier series
end
plot(x,s,'-*r')
s=a0/2;
subplot(2,2,2)
title(" fourier series expansion of first 10 terms")
for i=1:10
function b=f1(x, f)
b=f(x)*cos(i*x);
endfunction
function c=f2(x, f)
c=f(x)*sin(i*x);
endfunction
A(i)=1/%pi*intg(0,2*%pi,f1,.0000001);
B(i)=1/%pi*intg(0,2*%pi,f2,.0000001);
s=s+A(i)*cos(i*x)+B(i)*sin(i*x);
end
plot(x,s,'-+k')
s=a0/2;
subplot(2,2,3)
title(" fourier series expansion of first 100 terms")
for i=1:100
function b=f1(x, f)
b=f(x)*cos(i*x);
endfunction
function c=f2(x, f)
c=f(x)*sin(i*x);
endfunction
A(i)=1/%pi*intg(0,2*%pi,f1,.0000001);
B(i)=1/%pi*intg(0,2*%pi,f2,.0000001);
s=s+A(i)*cos(i*x)+B(i)*sin(i*x);
end
plot(x,s,'-*b')
s=a0/2;
subplot(2,2,4)
title("fourier series expansion of first 500 terms")
for i=1:500
function b=f1(x, f)
b=f(x)*cos(i*x);
endfunction
function c=f2(x, f)
c=f(x)*sin(i*x);
endfunction
A(i)=1/%pi*intg(0,2*%pi,f1,.0000001);
B(i)=1/%pi*intg(0,2*%pi,f2,.0000001);
s=s+A(i)*cos(i*x)+B(i)*sin(i*x);
end
plot(x,s,'-g')

You might also like