How to extract transfer function coefficient from symbolic functions?

I'm new in matlab. I have a college project to plot frequency response of user-defined transfer function with GUI by using freqz and my self-made function DTFTIIR (num,den,N) then compare the results. The user will input a function like H[n]= (0.5^n)*u[n] or H[z]=(1+z^-1+z^-2+z^-3)/(1-(0.18*z^-1)+(0.81*z^-2)). First, i think i will declare n and z as symbols. For n-domain functions i think i will use ztrans which will also resulting a symbolic equation in z domain. The problem is, how can i find the numerator and denumerator coefficient matrix from symbolic functions like (1+z^-1+z^-2+z^-3)/(1-(0.18*z^-1)+(0.81*z^-2)?

 Accepted Answer

You can do everyting you want in a fairly straightforward fashion. There are probably different ways to achieve your ultimate goal, but this is how I would do it.
The Symbolic Toolbox normalises and simplifies your function, and puts them in descending powers of z.
After that, the numden function is your friend here:
syms z
f = (1+z^-1+z^-2+z^-3)/(1-(0.18*z^-1)+(0.81*z^-2))
[nf, df] = numden(f)
yields:
nf =
100*z^3 + 100*z^2 + 100*z + 100
df =
z*(100*z^2 - 18*z + 81)
then sym2poly:
tfn = sym2poly(nf)
tfd = sym2poly(df)
yield:
tfn =
100.0000e+000 100.0000e+000 100.0000e+000 100.0000e+000
tfd =
100.0000e+000 -18.0000e+000 81.0000e+000 0.0000e+000
then create your transfer function with tf:
H = tf(tfn, tfd)
yields:
H =
100 s^3 + 100 s^2 + 100 s + 100
-------------------------------
100 s^3 - 18 s^2 + 81 s
Continuous-time transfer function.
NOTE: Your code specified a discrete-time transfer function, but tf will only provide that if you specify a sampling period, Ts, in seconds (although you can set Ts to -1 to leave the sample time unspecified).

4 Comments

thanks! your answer helping me a lot. i really didn't know there is a function like numden before..
My pleasure!
I started using MATLAB for a Linear Control course about two decades ago, so it and I have a history. I also used the Symbolic Math Toolbox to help me derive my transfer functions. It saved me a lot of time by avoiding the algebra errors I’d otherwise have likely made.
Also, there are several options in the tf and related functions, so if you want to read your participants’ responses as either continuous or discrete representations, you can likely test for those and do the appropriate operations. I also suggest that if you detect that the variable is ‘z’ that you then prompt for a sampling period, ‘Ts’.
you sound so expert about this, sir. on the contrary, this is my first matlab project. haha
got it. i think i will try those options. thanks again.
I wouldn’t consider myself an expert, but MATLAB and I go back a ways. I have my strengths, though.
The options are simply features of the Control System Toolbox functions that you can use to make your code as robust as possible.
My pleasure!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!