Fourier series doesn't approximate with bigger number of points
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello.
I am trying to approximate a function using fourier series, but using a larger input of points N I get an aproximation bigger than the original graph. I can't seem to find where the problem is any help is appreciated. First plot is the function, then it's the fourier series versus precise graph and then An and Bn koeficient correlation from N.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/926854/image.png)
code:
clc
clear
%conditions
T=3;
h=0.1;
a=-3;
b=3;
N=36;
x=a:h:b;
n1=0;
%original function
for iks=a:h:b
n1=n1+1;
graf1(1,n1)=iks;
graf1(2,n1)=funk(iks);
end
%plot
plot(graf1(1,:),graf1(2,:));
ylabel('y')
xlabel('x')
%Fourier series and aproximation
A0=0;
for iks2=0:h:T
A0=A0+funk(iks2);
end
A0=A0.*2.*h./T;
AnBn=zeros(3,N);
for nr=1:N
for n2=-T:h:T
AnBn(1,nr)=AnBn(1,nr)+funk(n2)*cos(2*nr*pi*n2/T);
AnBn(2,nr)=AnBn(2,nr)+funk(n2)*sin(2*nr*pi*n2/T);
end
end
AnBn=AnBn.*h./T;
n3=0;
aprox1=zeros(2,(b-a)./h);
for iks3=a:h:b
aprox=0;
for k=1:N
aprox=aprox+(AnBn(1,k).*cos(2*k*pi*iks3/T)+AnBn(2,k).*sin(2*k*pi*iks3/T));
end
aprox=aprox+A0/2;
n3=n3+1;
aprox1(1,n3)=iks3;
aprox1(2,n3)=aprox;
end
%ploting
figure
plot(aprox1(1,:),aprox1(2,:),'b',graf1(1,:),graf1(2,:),'r');
ylabel('y')
xlabel('x')
legend('Aproksimated f','Original f')
for nr1=1:N
AnBn(3,nr1)=nr1;
end
figure
scatter(AnBn(3,:),AnBn(1,:),'r')
ylabel('An')
xlabel('N')
figure
scatter(AnBn(3,:),AnBn(2,:))
ylabel('Bn')
xlabel('N')
funk:
function [y] = funk(x)
T=3;
x1=1.0;
x2=x1+0.5*pi;
if (x<0)
while (x<0)
x=x+T;
end
elseif (x>T)
while (x>T)
x=x-T;
end
end
if (0<=x && x<x1)
y=0;
elseif (x1<=x && x<=x2)
y=sin(2*(x-x1)).^2;
elseif (x2<x && x<=T)
y=0;
end
end
0 commentaires
Réponses (1)
Benjamin Thompson
le 14 Mar 2022
The fourier series coefficients should be divided by pi when calculating the reconstruction:
2 commentaires
Voir également
Catégories
En savoir plus sur Graphics Objects dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!