Why i get different output?

2 views (last 30 days)
aerun martin
aerun martin on 24 Dec 2014
Answered: Star Strider on 24 Dec 2014
n=-10:10;
g1=-2*(n<=-5);
g2=n.*(n==-4);
g3=n.*(n==-3);
g4=n.*(n==-2);
g5=n.*(n==-1);
g6=n.*(n==0);
g7=(4./n).*(n>=1);
G=g1+g2+g3+g4+g5+g6+g7;
subplot(3,2,1);
stem(n,G,'r','filled','linewidth',1)
xlabel('n');
ylabel('G[n]');
title ('G[n] against n');
this is my code. i suppose to get as below.

Answers (1)

Star Strider
Star Strider on 24 Dec 2014
There may be better solutions, but the easiest way I can think of the avoid the NaN at 0 is simply to add eps to ‘n’ in the denominator of ‘g7’:
g7=(4./(n+eps)).*(n>=1);
since that is generating it.

Community Treasure Hunt

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

Start Hunting!