From 86865a22aca8aa01f72a96a1586ff761e5acdc3f Mon Sep 17 00:00:00 2001 From: Allan CORNET Date: Tue, 17 Oct 2023 20:27:33 +0200 Subject: [PATCH] tf, ss mpower --- .../control_system/functions/@ss/display.m | 2 +- .../control_system/functions/@ss/mldivide.m | 12 ++-- modules/control_system/functions/@ss/mpower.m | 32 ++++++++++ .../control_system/functions/@tf/display.m | 4 +- .../control_system/functions/@tf/mldivide.m | 12 ++-- modules/control_system/functions/@tf/mpower.m | 34 +++++++++++ modules/control_system/functions/@tf/tf.m | 2 +- modules/control_system/tests/test_ss_mpower.m | 59 +++++++++++++++++++ modules/control_system/tests/test_tf_mpower.m | 47 +++++++++++++++ 9 files changed, 188 insertions(+), 16 deletions(-) create mode 100644 modules/control_system/functions/@ss/mpower.m create mode 100644 modules/control_system/functions/@tf/mpower.m create mode 100644 modules/control_system/tests/test_ss_mpower.m create mode 100644 modules/control_system/tests/test_tf_mpower.m diff --git a/modules/control_system/functions/@ss/display.m b/modules/control_system/functions/@ss/display.m index 067bfc3b6e..c230fa60d1 100644 --- a/modules/control_system/functions/@ss/display.m +++ b/modules/control_system/functions/@ss/display.m @@ -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]) diff --git a/modules/control_system/functions/@ss/mldivide.m b/modules/control_system/functions/@ss/mldivide.m index 228981708c..c3aebdb7a5 100644 --- a/modules/control_system/functions/@ss/mldivide.m +++ b/modules/control_system/functions/@ss/mldivide.m @@ -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 %============================================================================= diff --git a/modules/control_system/functions/@ss/mpower.m b/modules/control_system/functions/@ss/mpower.m new file mode 100644 index 0000000000..437103a937 --- /dev/null +++ b/modules/control_system/functions/@ss/mpower.m @@ -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 +%============================================================================= diff --git a/modules/control_system/functions/@tf/display.m b/modules/control_system/functions/@tf/display.m index 3296521ab2..1c47325a2c 100644 --- a/modules/control_system/functions/@tf/display.m +++ b/modules/control_system/functions/@tf/display.m @@ -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]; @@ -62,7 +62,7 @@ function display(varargin) numeratorString = deblank(numeratorString); denominatorString = deblank(denominatorString); dashString = getDashedLine(numeratorString, denominatorString); - + if (m ~= 1) if isOneString if ~haveNoNumerator diff --git a/modules/control_system/functions/@tf/mldivide.m b/modules/control_system/functions/@tf/mldivide.m index e382508ea8..1b9a202083 100644 --- a/modules/control_system/functions/@tf/mldivide.m +++ b/modules/control_system/functions/@tf/mldivide.m @@ -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 %============================================================================= diff --git a/modules/control_system/functions/@tf/mpower.m b/modules/control_system/functions/@tf/mpower.m new file mode 100644 index 0000000000..3a77e60433 --- /dev/null +++ b/modules/control_system/functions/@tf/mpower.m @@ -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 +%============================================================================= diff --git a/modules/control_system/functions/@tf/tf.m b/modules/control_system/functions/@tf/tf.m index 53f71e918d..94b2d90c02 100644 --- a/modules/control_system/functions/@tf/tf.m +++ b/modules/control_system/functions/@tf/tf.m @@ -8,6 +8,6 @@ % LICENCE_BLOCK_END %============================================================================= function sysOut = tf(sysIn) - sysOut = sysIn; + sysOut = sysIn; end %============================================================================= \ No newline at end of file diff --git a/modules/control_system/tests/test_ss_mpower.m b/modules/control_system/tests/test_ss_mpower.m new file mode 100644 index 0000000000..f0f58c373d --- /dev/null +++ b/modules/control_system/tests/test_ss_mpower.m @@ -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) +%============================================================================= diff --git a/modules/control_system/tests/test_tf_mpower.m b/modules/control_system/tests/test_tf_mpower.m new file mode 100644 index 0000000000..1736048399 --- /dev/null +++ b/modules/control_system/tests/test_tf_mpower.m @@ -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) +%=============================================================================