Filter löschen
Filter löschen

[MATLAB Grader] Comparing Transfer Functions

15 Ansichten (letzte 30 Tage)
BW
BW am 29 Jun. 2022
Kommentiert: Agustín am 15 Sep. 2023
Just wondering if there is a good way to compare transfer functions in MATLAB Grader?
I am testing a pseudo student response against the reference solution, purposely adding some slight deviation to check the test function.
Both the pseudo and reference solution output the same transfer function (at least to 4 dp), however from my testing, I believe the the condition:
abs(expected-actual)
cant be met, as it is the incorrect argument data type. Further to this,
(expected-actual)
creates an error function with higher order terms, I believe
minreal(expected-actual)
would make more sense.
Any help appreciated.
  10 Kommentare
Matt Rich
Matt Rich am 19 Jul. 2022
Bearbeitet: Matt Rich am 19 Jul. 2022
Yes, @BW @Paul excellent point about systems with poles on the imaginary axis! In those cases, one potential modification to the approach I can think of right now is to evaluate for some nonzero, conveniently chosen U which cancels the poles of the error system that are on the imaginary axis.
For example, form two "nearly identical" systems, each with 3 poles on the imaginary axis:
s = tf('s');
num = -7.0554*(s+2.919)*(s^2 + 0.04626*s + 0.3162);
den = s*(s+2.924)*(s+0.01636)*(s^2 + 0.5739*s + 6.944)*(s^2+1);
G1 = num/den
G1 = -7.055 s^3 - 20.92 s^2 - 3.184 s - 6.512 -------------------------------------------------------------------------- s^7 + 3.514 s^6 + 9.679 s^5 + 23.96 s^4 + 9.011 s^3 + 20.45 s^2 + 0.3322 s Continuous-time transfer function.
% duplicate the transfer function above introducing small errors
err = 100*eps;
num2 = (-7.0554 + err )*(s+2.919) *(s^2 + 0.04626*s + 0.3162);
den2 = s *(s+2.924) *(s+0.01636 + err) *(s^2 + 0.5739*s + 6.944)*(s^2+1+err);
G2 = num2/den2
G2 = -7.055 s^3 - 20.92 s^2 - 3.184 s - 6.512 -------------------------------------------------------------------------- s^7 + 3.514 s^6 + 9.679 s^5 + 23.96 s^4 + 9.011 s^3 + 20.45 s^2 + 0.3322 s Continuous-time transfer function.
Check (expected to be in this case since both systems have a pole on the imaginary axis)
norm(G1-G2,inf)
ans = Inf
Now, calculate the poles of each system, and extract those on the imaginary axis (allowing for some numerical error):
p1 = pole(G1);
p1imag = p1( abs(real(p1)) < 10*eps )
p1imag =
0.0000 + 0.0000i 0.0000 + 1.0000i 0.0000 - 1.0000i
p2 = pole(G2);
p2imag = p2( abs(real(p2)) < 10*eps )
p2imag =
0.0000 + 0.0000i 0.0000 + 1.0000i 0.0000 - 1.0000i
Take the union of the two sets of poles:
pImag = union(p1imag,p2imag)
pImag =
0.0000 + 0.0000i 0.0000 - 1.0000i 0.0000 + 1.0000i 0.0000 - 1.0000i 0.0000 + 1.0000i
Use this set of poles to create a "convenient" (in this case such that ) nonzero U:
U = zpk(pImag,-1*ones(size(pImag)) ,1)
U = s (s^2 + 1)^2 ------------- (s+1)^5 Continuous-time zero/pole/gain model.
U = tf(U)
U = s^5 - 1.11e-15 s^4 + 2 s^3 - 1.11e-15 s^2 + s --------------------------------------------- s^5 + 5 s^4 + 10 s^3 + 10 s^2 + 5 s + 1 Continuous-time transfer function.
Finally, check
norm((G1-G2)*U,inf)
ans = 2.7102e-11
I have not tested this too thoroughly yet with different amounts of error, MIMO systems, and need to think if there's a nice way to make it work for discrete without adding much code for logical statements but it seems promising.
BW
BW am 19 Jul. 2022
Bearbeitet: BW am 19 Jul. 2022
Thanks you once again,
Another option which worked in this case was the ratio of transfer functions, which should be close to unity (using minreal)

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Matt Rich
Matt Rich am 14 Jul. 2022
Bearbeitet: Matt Rich am 14 Jul. 2022
The most robust approach to assess LTI systems in MATLAB Grader is probably to use the norm of the difference between the reference and learner solution: for some small tolerance.
% check existence
assert( exist('G', 'var') , "The submission must contain a variable named G.")
% check class is a type of LTI system object
assert( isa(G,'tf') | isa(G,'ss') | isa(G,'zpk') , "The variable G must be an LTI system object (tf, ss, or zpk).")
% check IO size
assert( isequal( size(G), size(referenceVariables.G) ) , "G has has the wrong input/output dimensions.")
% check equivalence -- is L_inf norm of difference (error) close enough to zero?
tol = 1e-10;
assert( norm( referenceVariables.G - G , 'inf' ) < tol , "G is not correct.")
If you want to enforce the system is represented using a specific class (e.g., transfer function) then edit the class check:
% check class is tf
assert( isa(G,'tf') , "The variable G must be an transfer function.")
  2 Kommentare
BW
BW am 18 Jul. 2022
Excellent! Thanks all
Agustín
Agustín am 15 Sep. 2023
Could we compare both numerator and denominador of the transfer function instead of evaluating the L_inf norm? This might help students to identify where the error is.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by