Fourier Series Sum Code Check

10 views (last 30 days)
Dan Lardner
Dan Lardner on 15 May 2021
Commented: Jonas on 23 May 2021
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 Comments
Dan Lardner
Dan Lardner on 23 May 2021
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 on 23 May 2021
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!

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!