plot exponential Fourier series !!
41 次查看(过去 30 天)
显示 更早的评论
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 个评论
Neptune16
2019-11-10
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));
回答(3 个)
Baltam
2016-4-14
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
2023-2-20
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
0 个评论
Aman
2023-10-9
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spectral Measurements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!