How can I convert a transfer function object into a symbolic object for use with the Symbolic Math Toolbox in MATLAB R2023a?

151 views (last 30 days)
I have two transfer function objects in MATLAB R2023a. The first one, "sysd", was created using the System Identification Toolbox with the following code:
x = rand(100, 1);
y = 10*rand(100, 1) + 5;
np = 4; % number of poles
nz = 2; % number of zeros
Ts = 0.01; % time step
sysd = tfest(x, y, np, nz, 'Ts', Ts); % Get transfer function
The second transfer function object, "t", was created using the code:
 
t = tf(1:3,4:6)
Transfer function:
s^2 + 2 s + 3
---------------
4 s^2 + 5 s + 6
I want to convert both transfer function objects into symbolic objects for use with the Symbolic Math Toolbox. This will allow me to use functions like CCODE, FORTRAN, or LATEX on the transfer functions. How can I achieve this?
 
Please provide the necessary code for converting both "sysd" and "t" into symbolic objects and demonstrate how to use the CCODE, FORTRAN, or LATEX functions on the resulting symbolic expressions.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 26 Jun 2023
Edited: MathWorks Support Team on 13 Jul 2023
To convert the transfer function objects "sysd" and "t" into symbolic objects and use the CCODE, FORTRAN, or LATEX functions, you can follow the steps provided below:
For converting "sysd":
syms s
[num, denom] = tfdata(sysd); % extract numerator and denominator
sys_sym = poly2sym(cell2mat(num), s) / poly2sym(cell2mat(denom), s); % convert to sym
% Using CCODE, FORTRAN, and LATEX functions
c = ccode(sys_sym);
f = fortran(sys_sym);
l = latex(sys_sym);
For converting "t":
[num,den] = tfdata(t);
syms s
t_sym = poly2sym(cell2mat(num), s) / poly2sym(cell2mat(den), s);
% Using CCODE, FORTRAN, and LATEX functions
c = ccode(t_sym);
f = fortran(t_sym);
l = latex(t_sym);
 

More Answers (0)

Community Treasure Hunt

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

Start Hunting!