How to get z transfer function from difference equation?

7 views (last 30 days)
Hi guys, I have a difference equation:
How can i calculate its Z-transfer. I have calculated by hand but i want to know the methods of Matlab as well
And is it possible to transform G(z) into a continuous time system G(s) with matlab?
close all
clear all
clc
syms y(k) z x;
f = y(k)-1.238*y(k-1)+0.3016*y(k)-0.03175*x(k-2)+0.03175*x(k);
fZT = ztrans(f,k,z)
I do this but I have a error:
  1 Comment
Torsten
Torsten on 30 Nov 2022
Well, if you don't know that decimal numbers in MATLAB are written with . instead of , (thus e.g. 1.238 instead of 1,238), I suggest you first visit the MATLAB onramp course before you try to work on more complicated problems.

Sign in to comment.

Answers (2)

Jon
Jon on 29 Nov 2022
I don't think there is any direct way to convert a difference equation to a transfer function in MATLAB. Maybe it is possible with the symbolic toolbox but I am not familiar with this product.
It is not too difficult though to implement do this somewhat manually within MATLAB.
For example suppose we had the difference equation
a0*y(k) = a1*y(k-1) + a2*y(k-2) +b0*x(k) + b1*x(k-1) + b2*x(k-2)
Considering z^-1 as the delay operator we can rewrite this as
a0*y*z^0 = a1*y*z^-1 + a2*yz^-2 + b0*x*z^-0 + b1*x^z-1 + b2*x^z^-2
Multiplying through by z^2
a0*y*z^2 = a1*y*z + a2 + b0*x*z^2 +b1*x*z + b2
Rearranging
y/x = (b0* z^2 + b1*z + b2)/(a0*z^2 - a1*z - a2)
So in MATLAB we could just write
sys = tf([b0 b1 b2],[a0 -a1 -a2],tsample)
I think you can see the general pattern here, and realize that you don't have to acutally go through all of that algebra, but can simply write down the numerator and denominator polynomial (in z) coefficients directly from the original difference equation.

Paul
Paul on 30 Nov 2022
Hi Vivianne,
One workflow would go like this:
Define the variables and signals. Note that x is a function of k
syms y(k) z x(k);
Define f as the difference equation. Use decimal points, not commas. y(k) is on both sides of the equation? That seems like a typo, but we'll go with it for now and leave it as an exercise if it needs to be corrected:
f = y(k) == 1.238*y(k-1) - 0.3016*y(k) + 0.03175*x(k-2) - 0.03175*x(k);
Take the z-transform of both sides of the equation
fZT = ztrans(f,k,z)
fZT = 
Define Y(z) and X(z)
syms Y(z) X(z)
and substitute for the explicit expressions
fZT = subs(fZT,[ztrans(y(k),k,z) ztrans(x(k),k,z)],[Y(z) X(z)])
fZT = 
Becasuse we want the transfer function, the initial conditions are zero
fZT = subs(fZT,[y(-1) x(-1) x(-2)],[0 0 0])
fZT = 
Solve for Y(z)
Y(z) = isolate(fZT,Y(z))
Y(z) = 
For the transfer function, X(z) is the z-transform of the unit pulse, which is unity. Call the transfer function H(z)
H(z) = subs(Y(z),X(z),1)
H(z) = 
Seems like a lot of work when in most case the result can be written down by inspection, though in this case a bit more by-hand work is required because y(k) shows up in the equation twice.
As to the second part of your question, you could use numden to get the numerator and denominator polynomials, then use sym2poly to turn the symbolic polynomials into their numerical representations, then use tf to define a discrete-time transfer function, then use d2c to convert to a continuous-time transfer function.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by