Fourier series of any function

조회 수: 5 (최근 30일)
Danny Esteban Garzón Melo
Danny Esteban Garzón Melo 2020년 9월 17일
댓글: Danny Esteban Garzón Melo 2020년 9월 17일
Hello, I'm trying to build a simple script that finds the geometric fourier series of agiven function and a given period, everything seems to be good, but the plot is taking the absolute value, what do you think is the mistake?
f= input('ingrese la funcion, usando: @(t)LA_FUNCION: '); %ingresa la funcion como parametro
T= input('ingrese el periodo T: '); %ingresa el periodo como parametro
N= input('ingrese el numero de iteraciones N: '); %ingresa N como parametro
w0 = 2*pi/T;
suma=@(t) integral(f,-T/2,T/2)/2;
for k=1:N
fun_a=@(t) f(t).*cos(k*w0*t);
fun_b=@(t) f(t).*sin(k*w0*t);
a_k=2/T*integral(fun_a,-T/2,T/2); % calculo del coeficiente a
b_k=2/T*integral(fun_b,-T/2,T/2); %calculo del coeficiente b
suma= @(t) suma(t) +a_k*fun_a(t) + b_k*fun_b(t); %sumatoria de la serie de Fourier
end
t= -T/2:0.01:T/2;
x_t=f(t);
x_Nt=suma(t);
plot(t,x_t,t,x_Nt)
I tried several tests with square(t) and sawtooth(t) , wich period is 2*pi, but this is what I get: using n=1 and n=10
with n=1 with n=10 sawtooth with n=10
the Script somehow is doing the right thing, what do you think is wrong?,
thank you in advice
  댓글 수: 1
VBBV
VBBV 2020년 9월 17일
편집: Walter Roberson 2020년 9월 17일
plot(t,x_t.*(180/pi),t,x_Nt.*(180/pi));

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

채택된 답변

David Goodmanson
David Goodmanson 2020년 9월 17일
Hello Danny,
the problem is that you are supposed to approximate the function with a sum of sines and cosines with certain amplitudes. Your summation is not of sines and cosines, but rather in the case of cos,
b_k* f(t)*cos(k*w0*t)
and similarly for sine. So there is an extra factor of f(t). The following code removes that and gives a correct result..
Your method of summing the series is a bit unconventional but interesting, and it seems to work all right. But the computation time doesn't scale very well with the number of terms N. The time is increasing at something like N^2, so if you had, say, 2000 terms it will take awhile.
T = 10;
f = @(t) 6*(heaviside(t)-1/2); % for example
w0 = 2*pi/T;
N = 10;
suma=@(t) integral(f,-T/2,T/2)/2
for k=1:N
fun_a=@(t) f(t).*cos(k*w0*t);
fun_b=@(t) f(t).*sin(k*w0*t);
a_k=2/T*integral(fun_a,-T/2,T/2); % calculo del coeficiente a
b_k=2/T*integral(fun_b,-T/2,T/2); %calculo del coeficiente b
% suma= @(t) suma(t) +a_k*fun_a(t) + b_k*fun_b(t); %sumatoria de la serie de Fourier
suma= @(t) suma(t) +a_k*cos(k*w0*t) + b_k*sin(k*w0*t); %sumatoria de la serie de Fourier
end
t= -T/2:0.01:T/2;
x_t=f(t);
x_Nt=suma(t);
figure(1)
plot(t,x_t,t,x_Nt)
  댓글 수: 1
Danny Esteban Garzón Melo
Danny Esteban Garzón Melo 2020년 9월 17일
You are right :) , I was multiplying again by f(t) instead of using it only for the integration part, ofcourse this is not the optimal way to find such series, but it is an exercise, thank you very much.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by