Skip to content

Commit

Permalink
mrdivide tf, ss
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelson-numerical-software committed Oct 15, 2023
1 parent 4587a91 commit 6b4107d
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 0 deletions.
15 changes: 15 additions & 0 deletions modules/control_system/functions/@ss/mrdivide.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
%=============================================================================
% 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 = mrdivide(varargin)
sys1 = ss(varargin{1});
sys2 = ss(varargin{2});
varargout{1} = sys1 * inv(sys2);
end
%=============================================================================
72 changes: 72 additions & 0 deletions modules/control_system/functions/@tf/mrdivide.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
%=============================================================================
% 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 = mrdivide(varargin)
narginchk(2, 2);
nargoutchk(0, 1);
sysA = tf(varargin{1});
sysB = tf(varargin{2});
if ~issiso(sysA) && ~issiso(sysB)
error(_('SISO LTI model expected.'));
end
Ts = mrdivide_timesample(sysA.Ts, sysB.Ts);

numeratorA = sysA.Numerator{1}{1};
denominatorA = sysA.Denominator{1}{1};
numeratorB = sysB.Numerator{1}{1};
denominatorB = sysB.Denominator{1}{1};

if isempty(numeratorA) || isempty(numeratorB) || isempty(denominatorA) || isempty(denominatorB)
error(_('Matrix dimensions must agree.'));
end

if isstatic(sysA) && isstatic(sysA)
numerator = numeratorA / numeratorB;
sys = tf(numerator, 1, Ts);
else
numerator = conv(numeratorA, denominatorB);
denominator = conv(denominatorA, numeratorB);
sys = tf(numerator, denominator, Ts);
end

if isa(sysA, 'tf') && strcmp(sysA.Variable,'z^-1')
sys.Variable = 'z^-1';
elseif isa(sysB, 'tf') && strcmp(sysB.Variable,'z^-1')
sys.Variable = 'z^-1';
end

UserData = [];
if ~isempty(sysA.UserData) && ~isempty(sysB.UserData)
UserData = sysB.UserData;
else
if ~isempty(sysA.UserData)
UserData = sysA.UserData;
end
if ~isempty(sysA.UserData)
UserData = sysA.UserData;
end
end
if ~isempty(UserData)
sys.UserData = UserData;
end
varargout{1} = sys;
end
%=============================================================================
function Ts = mrdivide_timesample(TsA, TsB)
Ts = TsA;
if Ts ~= TsB
if ((Ts > 0) && (TsB > 0))
error(_('Sampling times must agree.'));
end
if (Ts < 0)
Ts = TsB;
end
end
end
%=============================================================================
16 changes: 16 additions & 0 deletions modules/control_system/tests/test_ss_mrdivide.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
%=============================================================================
% 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 = [1 2; 3 4];
B = [1 0; 0 1];
C = [1 1; 1 1];
D = [1 2; 3 4];
sys1 = ss(A, B, C, D);
sys2 = ss(A, B, C, D);
R = sy1 / sys2;
35 changes: 35 additions & 0 deletions modules/control_system/tests/test_tf_mrdivide.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
%=============================================================================
% 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
%=============================================================================
sysA = tf(3);
sysB = tf(4);
R = sysA / sysB;
REF = tf(0.75);
assert_isequal(R, REF);
%=============================================================================
sysA = tf(3);
R = sysA / 3;
REF = tf(1);
assert_isequal(R, REF);
%=============================================================================
sysA = tf(3);
R = 3 / sysA;
REF = tf(1);
assert_isequal(R, REF);
%=============================================================================
num = [3 4];
den = [3 1 5];
Ts = 0.2;
sys = tf(num, den, Ts);
R = sys / sys;
num_REF = [9 15 19 20];
den_REF = [9 15 19 20];
REF = tf(num_REF, den_REF, Ts);
assert_isequal(R, REF);
%=============================================================================

0 comments on commit 6b4107d

Please sign in to comment.