Skip to content

Commit

Permalink
tf, ss mpower
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-numerical-software committed Oct 17, 2023
1 parent 02d8f4c commit 86865a2
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 16 deletions.
2 changes: 1 addition & 1 deletion modules/control_system/functions/@ss/display.m
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ function displayMatrix(M, name, format)
if isempty(M)
return
end
if numel(M) > 17
if numel(M) > 36
disp([' ', name, ' = ', sizeToString(size(M))])
else
display(M, [' ', name])
Expand Down
12 changes: 6 additions & 6 deletions modules/control_system/functions/@ss/mldivide.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
% LICENCE_BLOCK_END
%=============================================================================
function varargout = mldivide(varargin)
narginchk(2, 2),
nargoutchk(0, 1);
sysA = ss(varargin{1});
sysB = ss(varargin{2});
sys = inv(sysA) * sysB;
varargout{1} = sys;
narginchk(2, 2),
nargoutchk(0, 1);
sysA = ss(varargin{1});
sysB = ss(varargin{2});
sys = inv(sysA) * sysB;
varargout{1} = sys;
end
%=============================================================================
32 changes: 32 additions & 0 deletions modules/control_system/functions/@ss/mpower.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
%=============================================================================
% Copyright (c) 2023-present Allan CORNET (Nelson)
%=============================================================================
% This file is part of the Nelson.
%=============================================================================
% LICENCE_BLOCK_BEGIN
% SPDX-License-Identifier: LGPL-3.0-or-later
% LICENCE_BLOCK_END
%=============================================================================
function varargout = mpower(varargin)
narginchk(2, 2)
nargoutchk(0, 1)
sysA = ss(varargin{1});
n = varargin{2};
mustBeNumeric(n, 2);
mustBeInteger(n, 2);
mustBeScalarOrEmpty(n, 2);
mustBeNonempty(n, 2);
if (n == 0)
varargout{1} = ss(1);
return;
end
sys = sysA;
for k = 2:abs(n)
sys = sys * sysA;
end
if n < 0
sys = inv(sys);
end
varargout{1} = sys;
end
%=============================================================================
4 changes: 2 additions & 2 deletions modules/control_system/functions/@tf/display.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function display(varargin)
haveNoNumerator = isempty(numeratorString);
denominatorString = stringPoly(denominator{k, q}, TF.Variable);
isOneString = strcmp(denominatorString, ' 1');

maxLen = max([length(denominatorString), length(numeratorString)]);
spacesToAdd = repmat(' ', 1, maxLen - length(numeratorString));
numeratorString = [spacesToAdd, numeratorString];
Expand All @@ -62,7 +62,7 @@ function display(varargin)
numeratorString = deblank(numeratorString);
denominatorString = deblank(denominatorString);
dashString = getDashedLine(numeratorString, denominatorString);

if (m ~= 1)
if isOneString
if ~haveNoNumerator
Expand Down
12 changes: 6 additions & 6 deletions modules/control_system/functions/@tf/mldivide.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
% LICENCE_BLOCK_END
%=============================================================================
function varargout = mldivide(varargin)
narginchk(2, 2),
nargoutchk(0, 1);
sysA = tf(varargin{1});
sysB = tf(varargin{2});
sys = inv(sysA) * sysB;
varargout{1} = sys;
narginchk(2, 2),
nargoutchk(0, 1);
sysA = tf(varargin{1});
sysB = tf(varargin{2});
sys = inv(sysA) * sysB;
varargout{1} = sys;
end
%=============================================================================
34 changes: 34 additions & 0 deletions modules/control_system/functions/@tf/mpower.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
%=============================================================================
% Copyright (c) 2023-present Allan CORNET (Nelson)
%=============================================================================
% This file is part of the Nelson.
%=============================================================================
% LICENCE_BLOCK_BEGIN
% SPDX-License-Identifier: LGPL-3.0-or-later
% LICENCE_BLOCK_END
%=============================================================================
function varargout = mpower(varargin)
narginchk(2, 2)
nargoutchk(0, 1)
sysA = tf(varargin{1});
n = varargin{2};
mustBeNumeric(n, 2);
mustBeInteger(n, 2);
mustBeScalarOrEmpty(n, 2);
mustBeNonempty(n, 2);
if (n == 0)
varargout{1} = ss(1);
return;
end

if (n < 0)
n = -n;
sysA = inv(sysA);
end
sys = sysA;
for i = 2:n
sys = sys * sysA;
end
varargout{1} = sys;
end
%=============================================================================
2 changes: 1 addition & 1 deletion modules/control_system/functions/@tf/tf.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
% LICENCE_BLOCK_END
%=============================================================================
function sysOut = tf(sysIn)
sysOut = sysIn;
sysOut = sysIn;
end
%=============================================================================
59 changes: 59 additions & 0 deletions modules/control_system/tests/test_ss_mpower.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
%=============================================================================
% Copyright (c) 2023-present Allan CORNET (Nelson)
%=============================================================================
% This file is part of the Nelson.
%=============================================================================
% LICENCE_BLOCK_BEGIN
% SPDX-License-Identifier: LGPL-3.0-or-later
% LICENCE_BLOCK_END
%=============================================================================
A = [0 1; 0 0];
B = [0; 1];
C = [1 0];
D = 0;
sys = ss(A, B, C, D);
sys2 = sys ^ 3;
REF_A = [0 1 0 0 0 0;
0 0 1 0 0 0;
0 0 0 1 0 0;
0 0 0 0 1 0;
0 0 0 0 0 1;
0 0 0 0 0 0];
REF_B = [ 0; 0; 0; 0; 0; 1];
REF_C = [1 0 0 0 0 0];
REF_D = 0;
assert_isequal(sys2.A, REF_A)
assert_isequal(sys2.B, REF_B)
assert_isequal(sys2.C, REF_C)
assert_isequal(sys2.D, REF_D)
%=============================================================================
A = [0 1; 0 0];
B = [0; 1];
C = [1 0];
D = 0;
sys = ss(A, B, C, D);
sys2 = sys ^ -3;

REF_A = [0 1 0 0 0 0 0;
0 0 1 0 0 0 0;
0 0 0 1 0 0 0;
0 0 0 0 1 0 0;
0 0 0 0 0 1 0;
0 0 0 0 0 0 1;
1 0 0 0 0 0 0];
REF_B = [ 0; 0; 0; 0; 0; 0; -1];
REF_C = [0 0 0 0 0 0 1]
REF_D = 0;
REF_E = [1 0 0 0 0 0 0;
0 1 0 0 0 0 0;
0 0 1 0 0 0 0;
0 0 0 1 0 0 0;
0 0 0 0 1 0 0;
0 0 0 0 0 1 0;
0 0 0 0 0 0 0];
assert_isequal(sys2.A, REF_A)
assert_isequal(sys2.B, REF_B)
assert_isequal(sys2.C, REF_C)
assert_isequal(sys2.D, REF_D)
assert_isequal(sys2.E, REF_E)
%=============================================================================
47 changes: 47 additions & 0 deletions modules/control_system/tests/test_tf_mpower.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
%=============================================================================
% Copyright (c) 2023-present Allan CORNET (Nelson)
%=============================================================================
% This file is part of the Nelson.
%=============================================================================
% LICENCE_BLOCK_BEGIN
% SPDX-License-Identifier: LGPL-3.0-or-later
% LICENCE_BLOCK_END
%=============================================================================
num = [3 4];
den = [3 1 5];
Ts = 0.2;
sys = tf(num, den, Ts);
sys2 = sys ^ 3;
R = evalc('display(sys2)');
REF = '
sys2 =
27 z^3 + 108 z^2 + 144 z + 64
—————————————————————————————————————————————————————————
27 z^6 + 27 z^5 + 144 z^4 + 91 z^3 + 240 z^2 + 75 z + 125
Sample time: 0.2000 seconds
Discrete-time transfer function.
';
assert_isequal(R, REF)
%=============================================================================
num = [3 4];
den = [3 1 5];
Ts = 0.2;
sys = tf(num, den, Ts);
sys2 = sys ^ -3;
R = evalc('display(sys2)');
REF = '
sys2 =
27 z^6 + 27 z^5 + 144 z^4 + 91 z^3 + 240 z^2 + 75 z + 125
—————————————————————————————————————————————————————————
27 z^3 + 108 z^2 + 144 z + 64
Sample time: 0.2000 seconds
Discrete-time transfer function.
';
assert_isequal(R, REF)
%=============================================================================

0 comments on commit 86865a2

Please sign in to comment.