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 Center および File ExchangeProgramming についてさらに検索

タグ

製品


リリース

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by