Commit ebd78e6b authored by Jakob Gabriel's avatar Jakob Gabriel
Browse files

misc.ss.combineInputSignals: now works also with signal names with dots

parent 02b5817f
......@@ -27,6 +27,7 @@ function u = combineInputSignals(myStateSpace, t, varargin)
% read name of input names from myStateSpace and calculate input lengths
myInputName = misc.ss.removeEnumeration(myStateSpace.InputName);
myInputNameUnique = unique(myInputName, 'stable');
myInputNameUniqueValidFieldName = strrep(myInputNameUnique, '.', 'POINT');
myInputLength = zeros(size(myInputNameUnique));
for it = 1 : numel(myInputLength)
myInputLength(it) = sum(strcmp(myInputNameUnique{it}, myInputName));
......@@ -35,17 +36,25 @@ end
% search for input signals in varargin or use default value = 0 instead
myParser = misc.Parser();
for it = 1 : numel(myInputNameUnique)
myParser.addParameter(myInputNameUnique{it}, ...
myParser.addParameter(myInputNameUniqueValidFieldName{it}, ...
quantity.Discrete(zeros(numel(t), myInputLength(it)), ...
'grid', t, 'gridName', 't', 'name', myInputNameUnique{it}), ...
'grid', t, 'gridName', 't', 'name', myInputNameUnique{it}), ...
@(v) isa(v, 'quantity.Discrete') & isvector(v) & (numel(v)==myInputLength(it)));
end
myParser.parse(varargin{:});
% replace '.' in varargin names, to obtain valid fieldnames
newVarargin = varargin;
for it = 1 : numel(newVarargin)
if ischar(newVarargin{it}) || isstring(newVarargin{it})
newVarargin{it} = strrep(newVarargin{it}, '.', 'POINT');
end
end
myParser.parse(newVarargin{:});
% finally, create resulting input vector
u = myParser.Results.(myInputNameUnique{1});
u = myParser.Results.(myInputNameUniqueValidFieldName{1});
for it = 2 : numel(myInputNameUnique)
u = [u; myParser.Results.(myInputNameUnique{it})];
u = [u; myParser.Results.(myInputNameUniqueValidFieldName{it})];
end % for
end % combineInputSignals()
\ No newline at end of file
......@@ -23,16 +23,27 @@ tc.verifyEqual(mySys.InputName, {'c.ode'});
end
function testCombineInputSignals(tc)
myStateSpace = ss(-1, [1, 2], 1, [], 'InputName', {'control', 'disturbance'});
myStateSpace = ss(-1, [1, 2, 3], 1, [], 'InputName', {'control', 'disturbance', 'my.fault'});
t = linspace(0, 4, 201);
disturbanceSignal = quantity.Symbolic(sin(sym('t')), 'grid', t, ...
'variable', sym('t'), 'name', 'disturbance');
faultSignal = quantity.Symbolic(sin(sym('t')), 'grid', t, ...
'variable', sym('t'), 'name', 'my.fault');
% case 1: only disturbance
u = misc.ss.combineInputSignals(myStateSpace, t, 'disturbance', disturbanceSignal);
y = quantity.Discrete(lsim(myStateSpace, u.on(), t), 'grid', t, ...
'gridName', 't', 'name', 'y');
odeResiduum = - y.diff(1, 't') - y + 2*disturbanceSignal;
tc.verifyEqual(odeResiduum.abs.median(), 0, 'AbsTol', 5e-4);
% case 2: fault and disturbance
u2 = misc.ss.combineInputSignals(myStateSpace, t, 'disturbance', disturbanceSignal, ...
'my.fault', faultSignal);
y2 = quantity.Discrete(lsim(myStateSpace, u2.on(), t), 'grid', t, ...
'gridName', 't', 'name', 'y');
odeResiduum = - y2.diff(1, 't') - y2 + 2*disturbanceSignal + 3*faultSignal;
tc.verifyEqual(odeResiduum.abs.median(), 0, 'AbsTol', 5e-4);
end
function testRemoveEnumeration(tc)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment