MATLAB: How can I find y[n]

dÄ°screte signal outputdiscrete time signal

I want to find output of system y[n] for n = 1,2,3,4 , y[n] is equal to y[n]=0.5*y[n-1] + x[n] + 0.5*x[n-3] .I have a knowledge about this system , system is LTI .I don't know right the code that below.
n = [1:4];
x = cos(pi.*n).*[0 ones(1,3)];
x_shifted=[0 x(1:end-1)];
x_scaled=2.*x;
% stem(n,x)
y(3) = 0;
y2(3) = 0;
for k=4:length(x)
y(k) = 0.5*y(k-1) + x_shifted(k) +0.5*x_shifted(k-1)+0.5*x_shifted(k-3);
y2(k)=0.5*y(k-1) + x(k) + 0.5*x(k-3);
end
stem(n,y,'rx')
hold on
stem(n,y2,'b')

Best Answer

% write your equation as : y(n)-0.5y(n-1) = x(n)+0x(n-1)+0x(n-2)+0.5x(n-3)
% and define the coefficient vectors a and b as shown.
n = 1:4;
a = [1 -0.5];
b = [1 0 0 0.5];
x = cos(pi.*n).*[0 ones(1,3)];
y = filter(b,a,x); % output of the LTI system
stem(n,y,'linewidth',3,'color','m')
grid;
a = title('Output of an LTI System y(n)');
set(a,'fontsize',14);
a = ylabel('y(n)');
set(a,'Fontsize',14);
a = xlabel('n [1 4]');
set(a,'Fontsize',14);
% figure
% stem(n,x,'linewidth',3,'color','g')