[MATLAB Grader] Comparing Transfer Functions
23 views (last 30 days)
Show older comments
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 Comments
Matt Rich
on 19 Jul 2022
Edited: Matt Rich
on 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
% 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
Check (expected to be ∞ in this case since both systems have a pole on the imaginary axis)
norm(G1-G2,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 )
p2 = pole(G2);
p2imag = p2( abs(real(p2)) < 10*eps )
Take the union of the two sets of poles:
pImag = union(p1imag,p2imag)
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 = tf(U)
Finally, check
norm((G1-G2)*U,inf)
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.
Accepted Answer
Matt Rich
on 14 Jul 2022
Edited: Matt Rich
on 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 Comments
Agustín
on 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.
More Answers (0)
See Also
Categories
Find more on Data Extraction in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!