Commit 75315ebc authored by Jakob Gabriel's avatar Jakob Gabriel
Browse files

misc.ss.combineInputSignals: new function to automatically combine input...

misc.ss.combineInputSignals: new function to automatically combine input signals that suite to the input names of a state space.
parent bbb11c84
function u = combineInputSignals(myStateSpace, t, varargin)
%MISC.SS.COMBINEINPUTSIGNALS reads the input varargin and searches for name-value-pairs that
%coincide with one of myStateSpace.InputName. If a pair is found, that signal is used
%as elements of u. For inputs that are not found, u-elements are set to zero.
%
% Inputs:
% myStateSpace state space for which an input signal is designed
% t time for which u shall be defined
% varargin name-value-pairs containing input signals with names coinciding
% with myStateSpace.InputName
% Outputs:
% u quantity vector of input signals suiting to myStateSpace for the
% time t.
%
% Example:
% -------------------------------------------------------------------------
% myStateSpace = ss(-1, [1, 2], 1, [], 'InputName', {'control', 'disturbance'});
% t = linspace(0, 4, 201);
% disturbanceSignal = quantity.Symbolic(sin(sym('t')), 'grid', t, ...
% 'variable', sym('t'), 'name', 'disturbance');
% u = misc.ss.combineInputSignals(myStateSpace, t, 'disturbance', disturbanceSignal);
% y = quantity.Discrete(lsim(myStateSpace, u.on(), t), 'grid', t, ...
% 'gridName', 't', 'name', 'y');
% plot([y; u]);
% -------------------------------------------------------------------------
% read name of input names from myStateSpace and calculate input lengths
myInputName = misc.ss.removeEnumeration(myStateSpace.InputName);
myInputNameUnique = unique(myInputName, 'stable');
myInputLength = zeros(size(myInputNameUnique));
for it = 1 : numel(myInputLength)
myInputLength(it) = sum(strcmp(myInputNameUnique{it}, myInputName));
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}, ...
quantity.Discrete(zeros(numel(t), myInputLength(it)), ...
'grid', t, 'gridName', 't', 'name', myInputNameUnique{it}), ...
@(v) isa(v, 'quantity.Discrete') & isvector(v) & (numel(v)==myInputLength(it)));
end
myParser.parse(varargin{:});
% finally, create resulting input vector
u = myParser.Results.(myInputNameUnique{1});
for it = 2 : numel(myInputNameUnique)
u = [u; myParser.Results.(myInputNameUnique{it})];
end % for
end % combineInputSignals()
\ No newline at end of file
......@@ -4,6 +4,19 @@ function [tests] = testSs()
tests = functiontests(localfunctions());
end
function testCombineInputSignals(tc)
myStateSpace = ss(-1, [1, 2], 1, [], 'InputName', {'control', 'disturbance'});
t = linspace(0, 4, 201);
disturbanceSignal = quantity.Symbolic(sin(sym('t')), 'grid', t, ...
'variable', sym('t'), 'name', '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);
end
function testRemoveEnumeration(tc)
tc.verifyEqual(misc.ss.removeEnumeration({'a(1)', 'a(2)', 'b'}), {'a', 'a', 'b'});
tc.verifyEqual(misc.ss.removeEnumeration({'a(1)', 'a(2)', 'b'}, false), {'a', 'a', 'b'});
......
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