You are on page 1of 2

University of Engineering and Technology Lahore

Department of Mechatronics and Control Engineering

LAB: Recursive Solution of Difference Equation

OBJECTIVE:
 To become familiar with developing recursive solution of difference equation.
 Also see the behavior zero input and zero state model.

BACKGROUND:
A general Nth order difference equation is like this:
y[n] + a1 y[n-1] + …+ aN y[n-N] = b0 x[n] + b1 x[n-1] +…+ bM x[n-M]
We can re-write any linear, constant-coefficient difference equation in “recursive form” like this:
y[n] + ∑ ai y[n-i] = ∑ bi x[n-i]

TASK:
Solve the difference equation recursively: y[n] -1.5 y [n-1] +y [n-2] =2 x [n-2]
For x[n] = u[n] unit step
And initial conditions are y[-2] = 2; y[-1] = 1
Recursive form: y[n] =1.5 y [n-1] - y [n-2] + 2 x [n-2]
Hints: solve scalar method or make function to solve difference equation.
In case of zero input model, switch all inputs to zero and in case of zero state, do all initial conditions to
zero.

COMMANDS:
length, if-else condition, for loop.

COMMENTS:

________________________________________________________________________________________________________________________________

________________________________________________________________________________________________________________________________

________________________________________________________________________________________________________________________________

TASK CODE:

Code 1:
4
x 10 4
44 x 10
N=100;
A=[ 1 0.5 -0.3 1]; 33

B=[0.8 -1 -0.5]; 22
Y_mem=[ -1.2 10 1.5];
x=[1 zeros(1,N-1)]; 11

X_mem=[1 1 1 1]; 00

M=length(Y_mem); -1
-1
if (length(B) ~= length(Y_mem))
-2
return -2

else -3
-30
0 10
10 20
20 30
30 40
40 50
50 60
60 70
70 80
80 90
90 100
100
X_mem=zeros(1,length(A));
for i=1:N
MCT 413 : Discrete Signal Processing
Department of Mechatronics and Control Engineering, U.E.T. Lahore
University of Engineering and Technology Lahore
Department of Mechatronics and Control Engineering

y(i)=(Y_mem*B')+(X_mem*A')
Y_mem=[y(i) Y_mem(1:end-1)];
X_mem=[x(i) X_mem(1:end-1)]
end
end
plot(y)

Code 2:

function y=recursive(x,y_ic)
y(1)=y_ic(1);
y(2)=y_ic(2);
for k=3:(length(x)+2)
y(k)=1.5*y(k-1)-y(k-2)+2*x(k-2)
end
% In new m-file
y_ic(1)=1;
y_ic(2)=2;
x=[0 0 ones(1,20)];
y=recursive(x,y_ic)
stem(-2:length(y)-3,y)

Code 3:

clc,clear,close
y_ic(1)=1;
y_ic(2)=2;
% //////zero input /////////
x=[0 0 zeros(1,20)];
y=recursive(x,y_ic)
stem(-2:length(y)-3,y)

Code 4:

clc,clear,close
% //////zero state input //////////
y_ic(1)=0;
y_ic(2)=0;
x=[0 0 ones(1,20)];
y=recursive(x,y_ic)
stem(-2:length(y)-3,y)

MCT 413 : Discrete Signal Processing


Department of Mechatronics and Control Engineering, U.E.T. Lahore

You might also like