MATLAB Answers

Plot Fourier Series on MatLab

8 views (last 30 days)
Erick Samayoa
Erick Samayoa on 6 Apr 2020
Answered: Asvin Kumar on 10 Apr 2020
Trying to plot the first few terms of my fourier series function but I cant seem to resemble the original function for n values greater than two.
n=[2:2:12]; % define n as only even numbers
for f=1:length(n) % define f
for Time=1:length(t)
a_not=1/pi;
b_n=(1/2)*sin((pi*t(Time))/T);
fourier(Time)=a_not+b_n+sum((2/(pi*(1-n(f)^2)))*cos((n(f)*pi*t(Time))/T));
end
figure(2)
plot(t,fourier,t,y)
hold on
end

  1 Comment

darova
darova on 6 Apr 2020
You don't use ifi statement?

Sign in to comment.

Answers (1)

Asvin Kumar
Asvin Kumar on 10 Apr 2020
Your coefficients are right. The issue is that instead of adding the n-th cosine to the exiting variable ‘fourier’, you were overwriting it. Here’s a simpler, stripped down and vectorized version of your code:
n=2:2:12; % define n as only even numbers
a_not=2/pi;
b1 = 0.5;
fourier = a_not/2*ones(size(t)) +b1*sin(pi*t/T) ;
for f=1:length(n) % define f
fourier = fourier + 2 / (pi*(1-n(f)^2)) * cos(n(f)*pi*t/T);
end
plot(t,fourier)
If you’re looking for a simpler way to do this have a look at the example at https://www.mathworks.com/help/curvefit/fourier.html#FitFourierModelsExample-1

  0 Comments

Sign in to comment.