LAB # 09 Difference Equation

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

LAB # 09

DIFFERENCE EQUATION
LAB TASK:
Write a MATLAB code to find the output of the given difference equations .Also
attach the output waveform
1) y(n) - 5/3y(n-1) + 7/4 y(n-2) = 2x(n)
Given x(n) = (1/6)n * u(n). Assume initial conditions as y(-1) = 2, y(-2)=5
Output:
clear all;
close all;
clc;
% accept difference equation coefficients from input
b=input('enter the coefficient of input x(n):');
a=input('enter the coefficient of output y(n):');
y=input('enter the initial condition y(-1)), y(-2),...:');
% calculate initial condition using filtic
z=filtic(b,a,y);
%enter input sequence samples.
x= [1 1/6 1/36 1/2165 1/1296 1/7776];
n= 0:length(x) -1;
%time base for plotting
%calculate output using initial conditions
Yout=filter(b,a,x,z);
%display output sequence
disp('difference equation solution : y(n) :');
disp(Yout);
%plot input and output
subplot(2,1,1);
stem(n,x);
title('input sequence x(n)');
xlabel('n'); ylabel('x(n)');
subplot(2,1,2);

stem(n,Yout);
title('output sequence y(n)');
xlabel('n'); ylabel('y(n)');

enter the coefficient of input x(n):2


enter the coefficient of output y(n):[1 -5/3 7/4]
enter the initial condition y(-1)), y(-2),...:[2 5]
difference equation solution : y(n) :
-3.4167 -8.8611 -8.7338

input sequence x(n) / zohaib /13EE018

x(n)

0.9515 16.8716 26.4544

0.5

0.5

1.5

0.5

1.5

30

2.5
3
n
output sequence y(n)

3.5

4.5

3.5

4.5

y(n)

20
10
0
-10

2.5
n

(2) Y(n)-1/2 y(n-1)=x(n)


Given x(n) = u(n). Assume initial conditions as y(-1)=0

Output:
clear all;
close all;
clc;
% accept difference equation coefficients from input
b=input('enter the coefficient of input x(n):');
a=input('enter the coefficient of output y(n):');
y=input('enter the initial condition y(-1):');
% calculate initial condition using filtic
z=filtic(b,a,y);
%enter input sequence samples.
x= [1 1 1 1 1 1];
n= 0:length(x) -1;
%time base for plotting
%calculate output using initial conditions
Yout=filter(b,a,x,z);
%display output sequence
disp('difference equation solution : y(n) :');
disp(Yout);
%plot input and output
subplot(2,1,1);
stem(n,x);
title('input sequence x(n)');
xlabel('n'); ylabel('x(n)');
subplot(2,1,2);
stem(n,Yout);
title('output sequence y(n)');
xlabel('n'); ylabel('y(n)');

enter the coefficient of input x(n):1


enter the coefficient of output y(n):[1 -1/2]
enter the initial condition y(-1):0

difference equation solution : y(n) :


1.0000

1.5000

1.8750

1.9375

1.9688

input sequence x(n) / zohaib khan / 13EE018

x(n)

1.7500

0.5

0.5

1.5

0.5

1.5

2.5
3
n
output sequence y(n)

3.5

4.5

3.5

4.5

y(n)

1.5
1
0.5
0

2.5
n

FILTIC:
filtic Make initial conditions for 'filter' function.
Z = filtic( B, A, Y, X ) converts past input X and output Y
into initial conditions for the state variables Z needed in the
TRANSPOSED DIRECT FORM II filter structure.
The vectors of past inputs & outputs are stored with more recent
values first.

You might also like