MATLAB: Discrete system solving with MATLAB

discrete system solving with matlabdoit4mehomework

Hello ! question : y[n]+1/4*y[n-1]-1/8y[n-2]=x[n] this discrete system , how do i solve it with matlab ? must i do z transform ? what is z transform code ? we will do h(z)=y(z)/x(z) and we find roots..??

Best Answer

you will need initials condition y(0) and y(-1); because Matlab don't allow negative index we 'll consider y(2) and y(1) as initials conditions
y(1)=0;
y(2)=0
k=0:20;
x=exp(-2*k); %your input system;
for n=3:numel(k)
y(n)=-(1/4)*y(n-1)+(1/8)*y(n-2)+x(n-2)
end
hold on
stem(k(1:end-2),y(3:end),'r');
Or you can use lsim command
N=[1 0 0];D=[1 1/4 -1/8];
te=1 % Sample time
model=tf(N,D,te);
lsim(model,x,k,[0 0])