Fourier Series Sum Code Check

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

if i see that correctly you should initialize your ft with 1/2 and then subtract the sum from n=1:100. at the moment you add the 1/2 term in each iteration
Hi Jonas,
Thanks for the reply. I'm not sure I follow you 100%, but assuming I have slightly changed the code which is below. I'ts getting closer, however with this code the amplitude is between -0.5 and 1.5. There is still an offset issue I'm clearly not understanding/identifying.
%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=1/2; %Define variables
n=1;
for n=1:100 %Count real numbers
%Fourier Transform written as a definite sum
ft=ft-(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;
xlabel('Interval (t)'), ylabel('f(t)'); %Set axis labels
Jonas
Jonas on 16 May 2021
Edited: Jonas on 16 May 2021
in my opinion you corrected your code corretly according to your formula!
i did not check the correctness of the fourie series, maybe the 1/2 term is wrong and should be 0, please check that. if i remember correctly the first part of the term is the integral over one period and since it is symmetrical over the y axis and one half is 0, this may be 0 in total
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
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 Mathematics in Help Center and File Exchange

Products

Release

R2017a

Asked:

on 15 May 2021

Commented:

on 23 May 2021

Community Treasure Hunt

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

Start Hunting!