plot exponential Fourier series !!

조회 수: 14 (최근 30일)
yazeed Alshorman
yazeed Alshorman 2016년 4월 14일
답변: Aman 2023년 10월 9일
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
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
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
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

Aman
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

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by