Skip to content

Instantly share code, notes, and snippets.

@cychitivav
Last active April 9, 2022 03:57
Show Gist options
  • Save cychitivav/4f03ef260bb565601cee6f6549a70f7e to your computer and use it in GitHub Desktop.
Save cychitivav/4f03ef260bb565601cee6f6549a70f7e to your computer and use it in GitHub Desktop.
Function to convert a symbolic expression to a transfer function.
function sys = sym2tf(symf,ts)
%TF2SYM Converts symbolic function to transfer function
% sys = SYM2TF(symf) converts the symbolic function symf of class 'sym'
% to transfer function.
%
% sys = SYM2TF(...,ts) converts the symbolic function s of class 'sym'
% to discrete transfer function.
%
% See also TF, SYM.
if length(symvar(symf)) > 1
error("only one symbolic variable ('s' or 'z') is allowed")
end
[N,D] = numden(symf);
N = sym2poly(N);
D = sym2poly(D);
if symvar(symf) == 's'
sys = tf(N,D);
elseif symvar(symf) == 'z'
if nargin < 2
error('You must specify the sampling time for a discrete-time transfer function')
end
sys = tf(N,D,ts);
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment