plot exponential Fourier series !!

132 views (last 30 days)
yazeed Alshorman
yazeed Alshorman on 14 Apr 2016
Answered: Aman on 9 Oct 2023
Hi, my instructor asked me to plot exponential Fourier series as homework, my homework say ( plot exponential Fourier series from -N to N and with amplitude A, the program will plot figure when N = 1 then for N=2 to N = value that user input it ) but I have problem in my code the problem is : when I put the limits of summation from -N to N it doesn't work except if I put step of for loop two, but if I but it one it doesn't work. please look to my code to understand line 13, this is my code :
clear;
clf;
A = input('Enter Ampitude A : ');
M = input('Enter limts of Summation N, where Summation limes -N:N : ');
T0 = 2;
wo = 2*pi/T0;
c0 = 0;
for i = 0:M
N = i;
t = -5:0.01:5;
figure(1)
y = c0*ones(size(t));
for n = -N:2:N, %<<<<<<<<<<<<<<< the problem is here, it can't be ( -N:1:N ) or ( -N:N )
cn = 2/(1j*n*wo);
y = y + A*real(cn*exp(1j*n*wo*t));
end
cla;
P = T0/2;
plot([-5 -4 -4 -3 -3 -2 -2 -1 -1 0 0 1 1 2 2 3 3 4 4 5],...
[-A -A A A -A -A A A -A -A A A -A -A A A -A -A A A], ':');
hold;
plot(t,y);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Exponential Fourier Series with N = ',...
num2str(N)];
title(ttle);
hold;
pause(0.5);
end
  1 Comment
Neptune16
Neptune16 on 10 Nov 2019
hello. can you please explain what is use for this lines?
y = c0*ones(size(t));
for n = -N:2:N, %<<<<<<<<<<<<<<< the problem is here, it can't be ( -N:1:N ) or ( -N:N )
cn = 2/(1j*n*wo);
y = y + A*real(cn*exp(1j*n*wo*t));

Sign in to comment.

Answers (3)

Baltam
Baltam on 14 Apr 2016
It's because you are dividing by zero. When n = 0 then cn is infinity. Because of that, matlab can't plot your result.
If you do n = -N:2:N you don't have this problem if N is odd.
Try using:
nvec = [-N:-1:-1,1:N];
for n = nvec %<<<<<<<<<<<<<<< the problem is here, it can't be ( -N:1:N ) or ( -N:N )
cn = 2/(1j*n*wo);
y = y + A*real(cn*exp(1j*n*wo*t));
end
in your code. This leaves out the zero-frequency component.

Muhammad Shoaib
Muhammad Shoaib on 20 Feb 2023
clear;
clf;
A = input('Enter Ampitude A : ');
M = input('Enter limts of Summation N, where Summation limes -N:N : ');
T0 = 2;
wo = 2*pi/T0;
c0 = 0;
for i = 0:M
N = i;
t = -5:0.01:5;
figure(1)
y = c0*ones(size(t));
for n = -N:2:N, %<<<<<<<<<<<<<<< the problem is here, it can't be ( -N:1:N ) or ( -N:N )
cn = 2/(1j*n*wo);
y = y + A*real(cn*exp(1j*n*wo*t));
end
cla;
P = T0/2;
plot([-5 -4 -4 -3 -3 -2 -2 -1 -1 0 0 1 1 2 2 3 3 4 4 5],...
[-A -A A A -A -A A A -A -A A A -A -A A A -A -A A A], ':');
hold;
plot(t,y);
xlabel('t (seconds)'); ylabel('y(t)');
ttle = ['Exponential Fourier Series with N = ',...
num2str(N)];
title(ttle);
hold;
pause(0.5);
end

Aman
Aman on 9 Oct 2023
clear; clf; A = input('Enter Ampitude A : '); M = input('Enter limts of Summation N, where Summation limes -N:N : '); T0 = 2; wo = 2*pi/T0; c0 = 0; for i = 0:M N = i; t = -5:0.01:5; figure(1) y = c0*ones(size(t)); for n = -N:2:N, %<<<<<<<<<<<<<<< the problem is here, it can't be ( -N:1:N ) or ( -N:N ) cn = 2/(1j*n*wo); y = y + A*real(cn*exp(1j*n*wo*t)); end cla; P = T0/2; plot([-5 -4 -4 -3 -3 -2 -2 -1 -1 0 0 1 1 2 2 3 3 4 4 5],... [-A -A A A -A -A A A -A -A A A -A -A A A -A -A A A], ':'); hold; plot(t,y); xlabel('t (seconds)'); ylabel('y(t)'); ttle = ['Exponential Fourier Series with N = ',... num2str(N)]; title(ttle); hold; pause(0.5); end

Community Treasure Hunt

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

Start Hunting!