# How to check if two transfer functions are the same?

66 views (last 30 days)
Bill Tubbs on 12 Aug 2020
Edited: Bill Tubbs on 7 Nov 2020
I want to verify that two transfer functions are equal.
For example:
Kc = 0.4262; Ti=1.35;
C1 = pidstd(Kc,Ti);
tf(C1)
s = tf('s');
Gc1 = Kc*(1+1/(Ti*s))
assert(tf(C1) == Gc1)
Raises:
Undefined operator '==' for input arguments of type 'tf'.
Also note:
Gc1
tf(C1)
Gc1 - tf(C1)
Gc1 =
0.5754 s + 0.4262
-----------------
1.35 s
Continuous-time transfer function.
ans =
0.4262 s + 0.3157
-----------------
s
Continuous-time transfer function.
ans =
0
Static gain.

Walter Roberson on 12 Aug 2020
Edited: Walter Roberson on 12 Aug 2020
isequal(tfdata(tf(C1) - Gc1), {[0]})
This will probably fail if there are delays in the tf.
Walter Roberson on 12 Aug 2020
tf are deliberately designed to commonly be used in arrays, Number of Inputs by Number of Outputs of them. So tfdata is designed to return the numerators of each of the tf array elements in distinguishable form, with the elements commonly not being the same length as each other. Rather than build a multidimensional array of coefficients with the first or last dimension being padded out to the longest numerator, and each element padded with leading zeros to match the longest numerator, the tfdata function simply creates a cell array of the numerators, like arrayfun of tfdata with 'uniform, 0. In the case of a scalar tf, that gives you a scalar cell array. tfdata does not then special case that scalar cell to extract the contents.

Bill Tubbs on 12 Aug 2020
Edited: Bill Tubbs on 12 Aug 2020
However, it does not work directly in my case:
isequal(tf(C1), Gc1)
ans =
logical
0
Need to factor the numerator and denominator so they are the same and then it works:
factor = Gc1.denominator{1}(1)
Gc1_factored = tf(Gc1.num{1}/factor,Gc1.den{1}/factor)
isequal(Gc1_factored,tf(C1))
factor =
1.3500
Gc1_factored =
0.4262 s + 0.3157
-----------------
s
Continuous-time transfer function.
ans =
logical
1
Bill Tubbs on 6 Nov 2020
I think it's worth highlighting that if using zpk during the equality check, it's important to also convert back to tf before the subtraction as Paul says in comment above. Otherwise, you can get some numerical errors creeping in.
E.g.
>> zpk(tf(1,[2 1])) - zpk(tf(1,[2 1]))
ans =
1.1102e-16
----------
(s+0.5)^2
Continuous-time zero/pole/gain model.
whereas
>> tf(zpk(tf(1,[2 1]))) - tf(zpk(tf(1,[2 1])))
ans =
0
Static gain.
Something to do with how subtraction is handled for zpk objects compared to tf objects perhaps.

Bill Tubbs on 6 Nov 2020
Edited: Bill Tubbs on 6 Nov 2020
Based on comments from Paul above I offer these functions as a solution:
function c = is_equal_tf(G1,G2)
c = almost_zero_static_gain_tf(G1 - G2);
end
function c = almost_zero_static_gain_tf(G)
[~,~,k] = zpkdata(G);
c = abs(k) < 1e-10;
end
Obviously, doesn't work with delays.
It passes the following tests:
assert(is_equal_tf(tf('s'),tf('s')))
assert(is_equal_tf(tf(1,1),tf(-1,-1)))
assert(is_equal_tf(tf(2,[2 1]),tf(1,[1 0.5])))
assert(~is_equal_tf(tf(2.1,[2 1]),tf(1,[1 0.5])))
assert(is_equal_tf(tf(2,conv([2 1],[5 1])), tf(2,[10 7 1])))
assert(~is_equal_tf(tf(2,conv([2 1],[4 1])), tf(2,[10 7 1])))
assert(is_equal_tf(tf(1,[2 1]) - tf(1,[5 1]), tf([3 0],[10 7 1])))
assert(is_equal_tf(pidstd(0.42,1.35), 0.42*(1+1/(1.35*tf('s')))))
assert(is_equal_tf(G1, G2))
G2.num{1} = G2.num{1}*pi;
G2.den{1} = G2.den{1}*pi;
assert(is_equal_tf(G1, G2))
Any exceptions or other cases I should test?
Bill Tubbs on 7 Nov 2020
The only way I know is to do it symbolically. There's a nice pair of functions, tf2sym and sym2tf in this package by Oskar Vivero Osornio to convert to/from symbolic functions. Then you can get it in a form where they are comparable by eye, as you suggest:
>> tf2sym(G1)
ans =
(34*s + 1)/(220*s^2 + 54*s + 2)
>> factor(tf2sym(G1))
ans =
[ 1/2, 34*s + 1, 1/(5*s + 1), 1/(22*s + 1)]
The thing I don't like with this approach is the need to continually switch between tf and symbolic, always redefining s in the same script.

R2019b

### Community Treasure Hunt

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

Start Hunting!