MATLAB: How to convert a transfer function object from the Control System Toolbox into a symbolic object for use with the Symbolic Math Toolbox

controlconvertsymsymbolicSymbolic Math Toolboxsystemtf

I create a transfer function object "t" using the following code:
t = tf(1:3,4:6)
Transfer function:
s^2 + 2 s + 3
---------------
4 s^2 + 5 s + 6
Can I now convert this into a symbolic object for use with the Symbolic Math Toolbox, so that it is possible to use the CCODE, FORTRAN, or LATEX functions with the transfer function?

Best Answer

To convert a transfer function object "t" into a symbolic object, use the following code:
t = tf(1:3,4:6);
[num,den] = tfdata(t);
syms s
t_sym = poly2sym(cell2mat(num),s)/poly2sym(cell2mat(den),s)
This results in:
t_sym =
(s^2+2*s+3)/(4*s^2+5*s+6)
From here, you can use Symbolic Math Toolbox functions, like CCODE, FORTRAN, or LATEX, on the t_sym object.
c = ccode(t_sym)
f = fortran(t_sym)
l = latex(t_sym)
This yields:
c =
t0 = (s*s+2.0*s+3.0)/(4.0*s*s+5.0*s+6.0);
f =
t0 = (s**2+2*s+3)/(4*s**2+5*s+6)
l =
{\frac {{s}^{2}+2\,s+3}{4\,{s}^{2}+5\,s+6}}
Related Question