Skip to content

Instantly share code, notes, and snippets.

@motchy869
Created October 31, 2022 03:00
Show Gist options
  • Save motchy869/db8fd116344d788f21d075f5b339cc68 to your computer and use it in GitHub Desktop.
Save motchy869/db8fd116344d788f21d075f5b339cc68 to your computer and use it in GitHub Desktop.
Approximate derivative operator in transfer function (tf) object
function sys_out = approxDeriv(sys_in, c)
% approxDeriv Replace `s` in tf object with `s/(c*s+1)` where `0 < c < 1`.
% Applying this operation, a non-proper transfer function can be
% approximated by a proper one.
%
% sys_out = approxDeriv(sys_in, c)
%
% ## Inputs
%
% sys_in: original tf object
% c: approximation factor
%
% ## Outputs
%
% sys_out: converted tf object
arguments
sys_in (1,1) tf
c (1,1) {mustBeInRange(c,0,1,"exclude-lower","exclude-upper")}
end
syms s
[coeffs_num,coeffs_den] = tfdata(sys_in);
num = poly2sym(coeffs_num, s);
den = poly2sym(coeffs_den, s);
T = symfun(num/den,s);
T_approx = simplifyFraction(subs(T,s,s/(c*s+1)));
[num2,den2] = numden(T_approx);
coeffs_num2 = sym2poly(num2);
coeffs_den2 = sym2poly(den2);
sys_out = tf(coeffs_num2,coeffs_den2);
end
sys_deriv = tf([1 0],1)
sys_approx = approxDeriv(sys_deriv, 0.001)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment