Equation for Fourier Series

8 次查看(过去 30 天)
I'm working on an assignment that requires me to plot a truncated fourier series. I have it very close to being correct.
The code I have now is
clc;
clear all;
close all;
f_0=400000;
t=0:0.1*10^(-6):6*10^(-6);
sum=0;
for n=1:5
sum=sum+4/((2*pi)*n-1)*sin((2*pi)*(2*n-1)*f_0*t);
f(n,:)=sum;
end
figure;
subplot(2,2,1);
plot(t,f(2,:),'LineWidth',2);
title('N=2');
xlabel('t in u sec');
ylabel('f(t)');
subplot(2,2,2);
plot(t,f(3,:),'LineWidth',2);
title('N=3');
xlabel('t in u sec');
ylabel('f(t)');
subplot(2,2,3);
plot(t,f(4,:),'LineWidth',2);
title('N=4');
xlabel('t in u sec');
ylabel('f(t)');
subplot(2,2,4);
plot(t,f(5,:),'LineWidth',2);
title('N=5');
xlabel('t in u sec');
ylabel('f(t)');
The graph I have now looks like
I need it to look like
I'm struggling to figure out why my graph is all wonky.
This is the actual problem I'm working in full:

采纳的回答

David Goodmanson
David Goodmanson 2021-4-11
编辑:David Goodmanson 2021-4-11
Hi Romain,
increasing the nomber of points is a good idea. I used
t=0:0.01*10^(-6):6*10^(-6);
but I believe there is also a problem with the code, which instead of
sum=sum+4/((2*pi)*n-1)*sin((2*pi)*(2*n-1)*f_0*t);
should be
sum=sum+4/(pi*(2*n-1))*sin((2*pi)*(2*n-1)*f_0*t);

更多回答(1 个)

Romain Boutant
Romain Boutant 2021-4-10
Your code is fine, you just have to increase the number of points to match the theoretical curves.
A simple fix is t=0:0.1*10^(-7):6*10^(-6); at line 5.

类别

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

Community Treasure Hunt

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

Start Hunting!

Translated by