Stuck on a piece of Code in Matlab

Thread Starter


Joined Mar 2, 2018
Hey everyone, im working on a project graphing unit impulse responses and zero input responses and what not from my textbook. Im stuck in the very last part where i have to plot the total response (which is simply the zero state plus the zero input response). Anyway, heres my code for all the responses including the final answer, i just need help plotting it. Ive never used matlab, and the assignment simply requires following the code given in the book, but they dont provide the final plot hence why im stuck.

>> b = [1 0 0]; a = [1 -1 1];
n = (0:40)'; delta = inline('n==0','n');
h = filter(b, a, delta(n));
stem(n,h,'k'); axis([-.5 30.5 -1.1 1.1]);
xlabel('n'); ylabel('h[n]');

x = inline ('cos (2*pi*n/6).*(n>=0)','n');
y = filter (b,a,x(n));
stem(n,y,'k'); xlabel('n'); ylabel('y[n]');

z_i = filtic (b,a,[1 2]);
y_0 = filter(b,a,zeros(size(n)), z_i);
stem(n, y_0, 'k'); xlabel ('n'); ylabel ('y_{0} [n]');
axis ([-0.5 30.5 -2.1 2.1]);

y_total = filter (b,a,x(n), z_i);
sum (abs(y_total - (y + y_0)))

ans =


stem (ans); xlabel ('n'); ylabel ('y[n]');

Also, even though i put n from o to 40 i still get that its from o to 30, i looked around and i cant find any errors that would give me this. Thanks everyone.


Joined Oct 2, 2009
The error is in your axis( ) statement.

axis ([-0.5 30.5 -2.1 2.1]);

axis( ) sets the xmin, xmax, ymin, ymax values.

Change your xmax value from 30.5 to a larger value such as 40.