[MATLAB Grader] Comparing Transfer Functions
Mostra commenti meno recenti
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 Commenti
Paul
il 14 Lug 2022
Perhaps @Bill Tubbs has found a battle tested solution he's willing to share in addition to that shown in the linked Question.
Bill Tubbs
il 14 Lug 2022
Im afraid i didnt. I asked my supervisors about this and they suggested to compare the step responses. The Solution by Matt Rich below looks better.
@Paul The
norm of the difference of the two transfer functions will return exactly zero whenever the accepted answer in the linked question would work. A nice advantage is that bounding it by a small tolerance will also account for the computational numerical precision issues, as well as the ss/zpk/tf or other class conversion precision issues mentioned there. Even better, it will work directly on ss or zpk or even a mixture of LTI system classes, it will work for systems with zeros, it will work for MIMO not just SISO, and will handle unstable systems. The only thing I can think of right now that would cause any problems is systems with internal delays.
@Bill Tubbs For stable SISO systems comparing the impulse or step response is going to be good too. I think it is probably the best way to go if you have delays.
For example:
sys1 = zpk([],-1,1)
sys2 = zpk(1,[-1 1],1)
Of course the difference from an L-inf norm perspective is going to be very small
norm(sys1-sys2,Inf)
But from a grading perspective, whether or not sys1 is equivalent to sys2 depends on whether or not the problem requires the student to perform the pole zero cancellation.
Perhaps a more concrete example would be
sys1 = tf(1,[1 1]);
sys2 = tf([1 2],[1 3]);
sysfb1 = feedback(sys1,sys2)
sysfb2 = sys1/(1 + sys1*sys2)
Here, sysfb1 and sysfb2 are identical by the L-inf test
norm(sysfb1 - sysfb2,Inf)
But, if the point of exercise is learn how to use feedback() instead of doing transfer function algebra in Matlab, then perhaps sysfb2 is to be considered incorrect.
And then of course we have to make sure the tolerance used in the L-inf test is smaller than the gain of the systems in question. Are sys1 and sys2 suppsed to be equal.
sys1 = 1e-10*tf(1,[1 1]);
sys2 = 1.1e-10*tf(1,[1 1]);
norm(sys1-sys2,inf)
Finally, as to your excellent point about delays, I think it really extends even to systems with input/ouput delays, because even those can become internal delays after the subtraction
sys1 = tf(1,[1 1],'InputDelay',1);
sys2 = sys1;
norm(parallel(sys1,-sys2),Inf) % works ok
sys2 = tf(1,[1 1],'InputDelay',2); % doesn't work ok
norm(parallel(sys1,-sys2),Inf)
BW
il 18 Lug 2022
Paul
il 19 Lug 2022
Shouldn't G1 and G2 be assessed as incorrect ? They don't look the same to me because -7.0554 ~= -0.035277.
But this brings up an excellent point about how to compare systems with poles on the imaginary axis using the L-inf norm.
BW
il 19 Lug 2022
Paul
il 19 Lug 2022
Ah, I didn't realize that the second tf was the difference G1 - G2.
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.
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.
Risposta accettata
Più risposte (0)
Categorie
Scopri di più su Model Attributes in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!