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

조회 수: 58(최근 30일)
Bill Tubbs 2020년 8월 12일
편집: Bill Tubbs 2020년 11월 7일
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 2020년 8월 12일
편집: Walter Roberson 2020년 8월 12일
isequal(tfdata(tf(C1) - Gc1), {})
This will probably fail if there are delays in the tf.
##### 댓글 수: 5표시숨기기 이전 댓글 수: 4
Walter Roberson 2020년 8월 12일
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.

댓글을 달려면 로그인하십시오.

### 추가 답변(2개)

Bill Tubbs 2020년 8월 12일
편집: Bill Tubbs 2020년 8월 12일
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
##### 댓글 수: 5표시숨기기 이전 댓글 수: 4
Bill Tubbs 2020년 11월 6일
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 2020년 11월 6일
편집: Bill Tubbs 2020년 11월 6일
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?
##### 댓글 수: 4표시숨기기 이전 댓글 수: 3
Bill Tubbs 2020년 11월 7일
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!