MATLAB code to solve Difference Equation.
  • Home
  • /
  • MATLAB code to solve Difference Equation.

MATLAB code to solve Difference Equation.


Consider the discrete-time LTI system characterized by the following difference equation with input and initial conditions specified:

 

y[n]  - 2 y[n-1] – 3 y[n-2]  = x[n] ,     with  y[0] = -and  y[1] = 0,    x[n] = (-1/2)n u[n-2].

 

·    Write a MATLAB program to simulate this difference equation.

You may try the commands filter or filtic or create a loop to compute the values recursively.

·    Printout and plot the values of the input signal, x[n] and the output signal, y[n] over the range 1 ≤ n ≤ 10.

·    Solve this difference equation by hand using the classical method.

·    Verify that the values of the output signal, y[n] produced by MATLAB are the same as those calculated by hand for the values of n = 2, 3, 4.

 


MATLAB Code
y0 = -1;
y(1) = 0;
y(2) = -2.75;
x0 = 0;
x(1) = 0;
x(2) = 0.25;
n = [1:1:10];
for m = 3:10
x(m) = (-0.5).^(m).*heaviside(m-2);
y(m) = x(m) + 2*y(m-1) + 3*y(m-2);
end
subplot(2,1,1)
stem(n,x)
xlabel('--------n----------->')
ylabel('-------x[n]------->')
title(' Plot of input signal x[n] over the
range from n=1 to n=10')
subplot(2,1,2)
stem(n,y)
xlabel('--------n----------->')
ylabel('-------y[n]------->')
title(' Plot of output signal y[n] over the
range from n=1 to n=10')