37 views (last 30 days)

Show older comments

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
on 11 Apr 2021

Edited: David Goodmanson
on 11 Apr 2021

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);

Romain Boutant
on 10 Apr 2021

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.

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

Start Hunting!