Code covered by the BSD License

# graphsymbols

### Daniel Auger (view profile)

15 Feb 2013 (Updated )

Compute the normalized left and right coprime graph symbols of a system.

testgraphsymbols.m
```%% testgraphsymbols
% Tests for the graphsymbols function.

%% Introduction
% Graph symbols are a mathematical construct that are useful in the
% analysis of uncertain systems in feedback control.  The function
% graphsymbols has been written to provide any easy way to compute them.
% Specifically, for some system P, it computes the Gl, a normalized left
% coprime graph symbol, Gr, a normalized right coprime graph symbol.
%
% This script was written to perform functional tests on the outputs of the
% graphsymbols function.  For each test case, we perform three checks:
%
% # The outputs are stable.
% # The outputs are normalized left/right coprime.
% # The outputs are graph symbols for the input.
%
% graphsymbols provides functionality that is not in Control System Toolbox
% or Robust Control Toolbox. It is compatible with both products.

%% Test with a scalar

% Define a plant:
P = ss(1);

% Form the graph symbols:
[Gl,Gr] = graphsymbols(P);

% Check stability of graph symbols.
assert(isstable(Gl));
assert(isstable(Gr));

% Check graph symbols normalized left and right coprime.
assert(norm(Gl * Gl' - eye(1), inf, 1e-4) < 2e-4)
assert(norm(Gr' * Gr - eye(1), inf, 1e-4) < 2e-4)

% For SISO systems we can check the graph symbols are consistent quite
% easily.
Ml = zpk(-Gl * [1; 0]);
Nl = zpk(Gl * [0; 1]);
Pl = minreal(zpk(Nl/Ml), [], false);

Nr = [1 0] * Gr;
Mr = [0 1] * Gr;
assert(isstable(Nr));
assert(isstable(Mr));
Pr = minreal(zpk(Nr/Mr), [], false);

% We can plot it all graphically to check the results make sense.
hFig = figure();
subplot(221)
bodemag(Gl, 'r-')
title('Norm. Left Graph Symbol');

subplot(224)
bodemag(Gr, 'g-')
title('Norm. Right Graph Symbol');

subplot(222)
sigma(P, 'b-', Pl, 'r--', Pr, 'g-.')
title('Comparison with Plant')

pause(2)
close(hFig);

%% Test with a double integrator with a non-minimum phase zero.
% This is a reasonably challenging plant.

% Define a plant:
P = tf([10 -1 1], [1 0 0]);

% Form the graph symbols:
[Gl,Gr] = graphsymbols(P);

% Check stability of graph symbols.
assert(isstable(Gl));
assert(isstable(Gr));

% Check graph symbols normalized left and right coprime.
assert(norm(Gl * Gl' - eye(1), inf, 1e-4) < 2e-4)
assert(norm(Gr' * Gr - eye(1), inf, 1e-4) < 2e-4)

% For SISO systems we can check the graph symbols are consistent quite
% easily.
Ml = zpk(-Gl * [1; 0]);
Nl = zpk(Gl * [0; 1]);
Pl = minreal(zpk(Nl/Ml), [], false);

Nr = [1 0] * Gr;
Mr = [0 1] * Gr;
assert(isstable(Nr));
assert(isstable(Mr));
Pr = minreal(zpk(Nr/Mr), [], false);

% We can plot it all graphically to check the results make sense.
hFig = figure();
subplot(221)
bodemag(Gl, 'r-')
title('Norm. Left Graph Symbol');

subplot(224)
bodemag(Gr, 'g-')
title('Norm. Right Graph Symbol');

subplot(222)
sigma(P, 'b-', Pl, 'r--', Pr, 'g-.')
title('Comparison with Plant')

pause(2)
close(hFig);

%% Test with a discrete system
% This is important - graphsymbols uses Tustin's Method and solve the
% problem in the continuous domain, so we need to check nothing's gone
% wrong.

% Define a plant:
P = tf([10 -1 1], [1 10 0]);
P = c2d(P, 0.001, 'zoh');

% Form the graph symbols:
[Gl,Gr] = graphsymbols(P);

% Check stability of graph symbols.
assert(isstable(Gl));
assert(isstable(Gr));

% Check graph symbols normalized left and right coprime.
assert(norm(Gl * Gl' - eye(1), inf, 1e-4) < 2e-4)
assert(norm(Gr' * Gr - eye(1), inf, 1e-4) < 2e-4)

% For SISO systems we can check the graph symbols are consistent quite
% easily.
Ml = zpk(-Gl * [1; 0]);
Nl = zpk(Gl * [0; 1]);
Pl = minreal(zpk(Nl/Ml), [], false);

Nr = zpk([1 0] * Gr);
Mr = zpk([0 1] * Gr);

Pr = minreal(zpk(Nr/Mr), [], false);

% We can plot it all graphically to check the results make sense.
hFig = figure();
subplot(221)
bodemag(Gl, 'r-')
title('Norm. Left Graph Symbol');

subplot(224)
bodemag(Gr, 'g-')
title('Norm. Right Graph Symbol');

subplot(222)
sigma(P, 'b-', Pl, 'r--', Pr, 'g-.')
title('Comparison with Plant')

pause(2)
close(hFig);

%% Test with a MIMO system
% This is a useful check that the dimensions in the matrix equations are
% all consistent.  (This is easy to get wrong, and we don't want run-time
% errors!)

% Define a plant:
% Setup a (non-square) plant transfer function
P1 = tf(1, [1 2 1]);
P2 = tf(1, [10 1 1]);
P3 = tf(1, [100 0.5 1]);
P = [P1 P2 0; 0 0 P3];

% Form the graph symbols:
[Gl,Gr] = graphsymbols(P);

% Check stability of graph symbols.
assert(isstable(Gl));
assert(isstable(Gr));

% Check graph symbols normalized left and right coprime.
[ny, nu] = size(P);
assert(norm(Gl * Gl' - eye(ny), inf, 1e-4) < 2e-4)
assert(norm(Gr' * Gr - eye(nu), inf, 1e-4) < 2e-4)

% For MIMO systems the equivalence check is slightly more complicated.  It
% can be done with feedback transformations, but here we'll do it a in a
% simpler way, by using matrices to 'select' the parts we're after.
[ny, nu] = size(P);
Ml = sminreal(-Gl * [eye(ny); zeros(nu, ny)]);
Nl = sminreal(Gl * [zeros(ny, nu); eye(nu)]);
Pl = minreal(Ml \ Nl, [], false);

Nr = sminreal([eye(ny) zeros(ny,nu)] * Gr);
Mr = sminreal([zeros(nu,ny) eye(nu)] * Gr);
Pr = minreal((Mr' \ Nr')', [], false);

% We can plot it all graphically to check the results make sense.
hFig = figure();
subplot(221)
bodemag(Gl, 'r-')
title('Norm. Left Graph Symbol');

subplot(224)
bodemag(Gr, 'g-')
title('Norm. Right Graph Symbol');

subplot(222)
sigma(P, 'b-', Pl, 'r--', Pr, 'g-.')
title('Comparison with Plant')

pause(2)
close(hFig);
```