Fourier Series Sum Code Check

10 次查看(过去 30 天)
Dan Lardner
Dan Lardner 2021-5-15
评论: Jonas 2021-5-23
Could I please ask for some assistance with the following piece of code.
I have manually calculated the fourier transform which is below. I deliberately chose n <= 100. The problem I am experiencing is the amplitude. I expect the the amplitude to be from -1 to 1 as I have proven this by performing a piecewise plot where the function was defined between -2 & 2, however it is only -0.4 to 0.4. If I change the first term in the equation from 1/2 to 1/100 I get the amplitude I am after, however I am wondering if there is something I have done with the code that I cannot see which is the reason the plot has a reduced amplitude using the derived equation. Maybe there is an issue with calculations, but I would appreciate if a far more experienced Matlab user could confirm if I have coded correctly.
%Fourier Series
%Function is defined on the interval -2<=t<=2
%f(t) =(-t-1) when -2<=t<=0 & (t-1) when 0<=t<=2
%Plot function over the interval -8<=t<=8
t=-8:2:8; %Define interval
ft=0; %Define variables
n=1;
while n<=100 %Count real numbers
%Fourier Transform written as a definite sum
ft=ft+((1/2)-(8/pi.^2))*cos((2*n-1)*pi*t/2)./(2*n-1).^2;
n=n+1;
end
plot(t,ft)
grid on;hold on;
  5 个评论
Dan Lardner
Dan Lardner 2021-5-23
Hi Jonas,
Thanks again for your feedback. Apologies for the late reply. I had another look through my calculations and I have indeed found my error. I have calculated my mean value incorrectly. It should have been zero instead of 1/2. The problem is now resolved.
Thanks again for your input.
Regards
Jonas
Jonas 2021-5-23
hi Dan, congratulations to your solution. now you can put your own developed answer (with corrected code and explanation) below your question and accept it as your own answer, then this questions is sorted into the category 'answered'. good work and good luck with your next task!

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by