Equation for Fourier Series

조회 수: 3 (최근 30일)
Brendan Clark
Brendan Clark 2021년 4월 10일
편집: David Goodmanson 2021년 4월 11일
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