how to plot fourier series in matlab

how to plot fourier series in matlab

댓글 수: 5

Abraham Boayue
Abraham Boayue 2018년 3월 16일
Have you done the integrals to fined the a0, an and bn? If so, what is the expression you got for the fourier series?
Akira Agata
Akira Agata 2018년 3월 16일
편집: Akira Agata 2018년 3월 16일
Regarding the question (1) in the picture, I would recommend try to calculate by hand first, for your better understanding of Fourier transformation of periodic function.
clear all;clc;
syms x n pi
% pi=3.14;
sum=0;
y=x %function you want
a0=(1/pi)*int(y,x,-pi,pi)
% for n=1:5
%finding the coefficients
an=(1/pi)*int(y*cos(n*x),x,-pi,pi)
bn=(1/pi)*int(y*sin(n*x),x,-pi,pi)
sum=a0/2+(an*cos(n*x)+bn*sin(n*x))
% end
ezplot(x,y,[-pi,pi])
grid on;hold on;
ezplot(x,(sum+a0/2),[-pi,pi])
% https://www.instagram.com/koroshkorosh1/
syms x pi
F =(1/pi) * int(x^2+5*x,'Hold',true)
bn=(1/pi)*int(y*sin(n*x),x,-pi,pi), G = bn
Gcalc = release(G)
Fcalc = int(bn)
clear all;clc;
syms x pi n
% pi=3.14;
sum=0;
y= x + (x^2) ; %function you want
a0=(1/pi)*int(y,x,-pi,pi)
% for n=1:3
%finding the coefficients
an=(1/pi)*int(y*cos(n*x),x,-pi,pi)
bn=(1/pi)*int(y*sin(n*x),x,-pi,pi)
sum=sum+(an*cos(n*x)+bn*sin(n*x))
% end
% https://www.instagram.com/koroshkorosh1/
ezplot(x,y,[-3.14,3.14]);
grid on;hold on;
ezplot(x,(sum+a0/2),[-3.14,3.14])
% https://www.instagram.com/koroshkorosh1/
Salihath
Salihath 2022년 12월 15일
이동: Rik 2022년 12월 16일
how to write matlab code for time modulated array
Rik
Rik 2022년 12월 16일
@Salihath Since your post was not an answer, I moved it to the comment section. Have a read here and here. It will greatly improve your chances of getting an answer.

댓글을 달려면 로그인하십시오.

답변 (6개)

Abraham Boayue
Abraham Boayue 2018년 3월 18일
편집: Abraham Boayue 2018년 6월 15일

2 개 추천

Here is what your Fourirer series would like if my calculations were made correctly. An attachment of the solution is also included for your reference. Take care for now.
clear variables
close all
% Fourier series of neither even nor odd function
% Decompose f(x) into even (fe) and odd (fo) functions.
% fe = (f(x) + f(-x))/2, fo = (f(x) - f(-x))/2
N = 500;
L = 4;
xd = -L:2*L/(N-1):L;
y1 = -1/8*xd.^2;
y2 = 1/8*xd.^2;
fo = y1.*(-L<=xd & xd<=0) +y2.*(0<=xd & xd<=L);
fe = 4-xd.^2/8;
f2 = fe + fo;
a0 = 10/3;
% Generate the fourier series of f(x)
y = zeros(1,N);
x = [];
K = 80;
for k = 1:K
ck = 1/(pi*k);
an = (2*L*(-1).^(k+1))*ck^2;
bn = L*(-1).^(k+1)*ck + (2*L*ck^3)*((-1)^k-1);
y = y + (an*cos(pi*k/L*xd)+ bn*sin(pi*k/L*xd)); % For fe and fo
x = [x;y];
end
y = a0 +y;
x = a0 +x;
% Color codes
s = distinguishable_colors(K); % search this function on mathworks
figure
subplot(121) % Plot f(t)
plot(xd,f2,'linewidth',2.5,'color',s(1,:))
line(xlim,[0 0],'color',s(6,:),'linewidth',3);
line([0 0],ylim,'color',s(6,:)','linewidth',3);
ylim([-.5 4]);
a= xlabel('\itt\rm (seconds)');
set(a,'fontsize',20);
a = ylabel('\itf\rm(\itt\rm)');
set(a,'fontsize',20);
a= title('f(t)');
set(a,'fontsize',14);
grid
subplot(122) % Plot fouries series of f(t);
hold on
q = length(x(:,1));
M = 1:q;
for i = 1:6:q
plot(xd,x(i,:),'linewidth',2.5,'color',s(i,:),'DisplayName',sprintf('S = %1.2f',M(i)))
end
a= title('Fourier series of f(t)');
set(a,'fontsize',14);
a= xlabel('\itt\rm (seconds)');
set(a,'fontsize',20);
a = ylabel('\itf\rm(\itt\rm)');
set(a,'fontsize',20);
line(xlim,[0 0],'color',s(6,:),'linewidth',3);
line([0 0],ylim,'color',s(6,:)','linewidth',3);
legend('-DynamicLegend','location','bestoutside');
grid

댓글 수: 1

clear all;clc;
syms x
pi=3.14;
sum=0;
y=exp(x); %function you want
a0=(1/pi)*int(y,x,-pi,pi);
for n=1:3
%finding the coefficients
an=(1/pi)*int(y*cos(n*x),x,-pi,pi);
bn=(1/pi)*int(y*sin(n*x),x,-pi,pi);
sum=sum+(an*cos(n*x)+bn*sin(n*x));
end
% https://www.instagram.com/koroshkorosh1/
ezplot(x,y,[-pi,pi]);
grid on;hold on;
ezplot(x,(sum+a0/2),[-pi,pi]);
% https://www.instagram.com/koroshkorosh1/

댓글을 달려면 로그인하십시오.

Abraham Boayue
Abraham Boayue 2018년 3월 18일

2 개 추천

The is the solution file, the math is a bit messy, but I assume that you are familiar with the material that you are studying.
Abhishek Ballaney
Abhishek Ballaney 2018년 3월 16일

0 개 추천

https://in.mathworks.com/help/curvefit/fourier.html

댓글 수: 2

clear all;clc;
syms x
pi=3.14;
sum=0;
y=exp(x); %function you want
a0=(1/pi)*int(y,x,-pi,pi);
for n=1:3
%finding the coefficients
an=(1/pi)*int(y*cos(n*x),x,-pi,pi);
bn=(1/pi)*int(y*sin(n*x),x,-pi,pi);
sum=sum+(an*cos(n*x)+bn*sin(n*x));
end
% https://www.instagram.com/koroshkorosh1/
ezplot(x,y,[-pi,pi]);
grid on;hold on;
ezplot(x,(sum+a0/2),[-pi,pi]);
% https://www.instagram.com/koroshkorosh1/
vikrant rana
vikrant rana 2022년 1월 26일
hey abhishek,
what would be the changes in code if y=(pi-x)/2
and the limits are from 0 to 2 pi
like i am trying to make changes in the code by substituiting my values i not happening.
i shall be thankful to you if you resolve my doubt.

댓글을 달려면 로그인하십시오.

Mohamed Abugammar
Mohamed Abugammar 2019년 4월 10일

0 개 추천

clc;
close all;
clear all;
dx=0.001;
L=pi;
x=(-1+dx:dx:1)*L;
n=length(x); nquart=floor(n/4);
% define the hat function;
f=0*x;
f(nquart:2*nquart)=4*(1:nquart+1)/n;
f(2*nquart+1:3*nquart)=1-4*[1:500]/n;
plot(x, f,'r','LineWidth', 2); hold on;
%% define the coffeciet
A0=sum(f.*ones(size(x)))*dx;
fFS = A0/2;
for k=1:10
Ak=sum(f.*cos(pi*k*x/L))*dx;
Bk=sum(f.*sin(pi*k*x/L))*dx;
fFS=fFS+Ak*cos(pi*k*x/L)+Bk*sin(pi*k*x/L);
plot(x,fFS);
pause(1); drawnow;
end

댓글 수: 1

clear all;clc;
syms x
pi=3.14;
sum=0;
y=exp(x); %function you want
a0=(1/pi)*int(y,x,-pi,pi);
for n=1:3
%finding the coefficients
an=(1/pi)*int(y*cos(n*x),x,-pi,pi);
bn=(1/pi)*int(y*sin(n*x),x,-pi,pi);
sum=sum+(an*cos(n*x)+bn*sin(n*x));
end
% https://www.instagram.com/koroshkorosh1/
ezplot(x,y,[-pi,pi]);
grid on;hold on;
ezplot(x,(sum+a0/2),[-pi,pi]);
% https://www.instagram.com/koroshkorosh1/

댓글을 달려면 로그인하십시오.

Dhiya Eid
Dhiya Eid 2020년 7월 20일

0 개 추천

Let f(x) be a 2π-periodic function such that f(x)=x2 for x∈[−π,π]. Find the Fourier series for the parabolic wave.
solve it in matlab

댓글 수: 2

Deniz Sezen
Deniz Sezen 2023년 4월 28일
Could you solve that question?
Rik
Rik 2023년 4월 29일
This is an 'answer' from almost 3 years ago. I doubt you will get a response.

댓글을 달려면 로그인하십시오.

Korosh Agha Mohammad Ghasemi
Korosh Agha Mohammad Ghasemi 2020년 9월 22일

0 개 추천

f=@(x)x.*(x>0 & x<-pi)-2*(x/pi+1).*(x>=-pi & x<=-pi/2);
n=50;
k=0:n;
a=1/pi*(integral(@(x)f(x).*cos(k*x),-pi,-pi/2,'ArrayValued',true)+integral(@(x)f(x).*cos(k*x),0,pi/2,'ArrayValued',true));
k=1:n;
b=1/pi*(integral(@(x)f(x).*sin(k*x),-pi,-pi/2,'ArrayValued',true)+integral(@(x)f(x).*sin(k*x),0,pi/2,'ArrayValued',true));
ffun=@(x)a(1)/2+sum(a(2:n+1).*cos((1:n)*x)+b(1:n).*sin((1:n)*x));
x=linspace(0,pi,200);
fx=arrayfun(@(x)ffun(x),x);
plot(x,fx,x,f(x))
% https://www.instagram.com/koroshkorosh1/

댓글 수: 3

clear all;clc;
syms x
pi=3.14;
sum=0;
y=exp(x); %function you want
a0=(1/pi)*int(y,x,-pi,pi);
for n=1:3
%finding the coefficients
an=(1/pi)*int(y*cos(n*x),x,-pi,pi);
bn=(1/pi)*int(y*sin(n*x),x,-pi,pi);
sum=sum+(an*cos(n*x)+bn*sin(n*x));
end
% https://www.instagram.com/koroshkorosh1/
ezplot(x,y,[-pi,pi]);
grid on;hold on;
ezplot(x,(sum+a0/2),[-pi,pi]);
% https://www.instagram.com/koroshkorosh1/
taha bandrawala
taha bandrawala 2020년 9월 22일
how to write if i want to solve f(x)=x^3 and find the fourier series of f(x)
Gülcan söm
Gülcan söm 2020년 12월 30일
how to write if i want to solve f(x)=cos(3x) and find the fourier series of f(x)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

질문:

2018년 3월 16일

댓글:

Rik
2023년 4월 29일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by