Fourier series doesn't approximate with bigger number of points

1 visualizzazione (ultimi 30 giorni)
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.
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

Risposte (1)

Benjamin Thompson
Benjamin Thompson il 14 Mar 2022
The fourier series coefficients should be divided by pi when calculating the reconstruction:
  2 Commenti
reddlno
reddlno il 15 Mar 2022
And that should fix it? Just dividing coefficients a0, an, ab by pi?
Torsten
Torsten il 15 Mar 2022
According to your graph, there is a factor 3 between the two curves ...

Accedi per commentare.

Categorie

Scopri di più su Graphics Objects in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by