comparison m-toolbox/classes/@ssm/parameterDiff.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % PARAMETERDIFF Makes a ssm that produces the output and state derivatives.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: PARAMETERDIFF Makes a ssm that produces the output
5 % and state derivative in regard with specified parameters, for a specificed variation.
6 %
7 % CALL: obj = obj.parameterDiff({'key1', ...}, [val1, ...]);
8 % obj = obj.parameterDiff(plist);
9 % obj = obj.parameterDiff('key', val);
10 %
11 % <a href="matlab:utils.helper.displayMethodInfo('ssm', 'parameterDiff')">Parameters Description</a>
12 %
13 % VERSION: $Id: parameterDiff.m,v 1.9 2011/04/08 08:56:22 hewitson Exp $
14 %
15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
16
17 function varargout = parameterDiff(varargin)
18
19 % Check if this is a call for parameters
20 if utils.helper.isinfocall(varargin{:})
21 varargout{1} = getInfo(varargin{3});
22 return
23 end
24
25 %% starting initial checks
26 utils.helper.msg(utils.const.msg.MNAME, ['running ', mfilename]);
27
28 % Collect input variable names
29 in_names = cell(size(varargin));
30 for ii = 1:nargin, in_names{ii} = inputname(ii); end
31
32 % Collect all SSMs and options
33 [sys, ssm_invars, rest] = utils.helper.collect_objects(varargin(:), 'ssm', in_names);
34 [pl, invars2, rest] = utils.helper.collect_objects(rest(:), 'plist');
35 if ~isempty(rest)
36 pl = combine(pl, plist(rest{:}));
37 end
38 pl = combine(pl, getDefaultPlist());
39
40 %%% Internal call: Only one object + don't look for a plist
41 internal = strcmp(varargin{end}, 'internal');
42
43 %% processing input
44 names = pl.find('names');
45 if ischar(names)
46 names = {names};
47 elseif ~iscellstr(names)
48 error('### Parameter names must be a cell-array of strings')
49 end
50
51 values = pl.find('values');
52 if ~isa(values, 'double')
53 error('### param values should be a double')
54 end
55
56 Nsys = numel(sys);
57 sys_out = ssm.initObjectWithSize(Nsys,1);
58
59 %% checking data
60 Ndiff = length(names);
61 if ~(Ndiff== length(values))
62 error(['### The number of parameter names is ' num2str(Ndiff) ' and the number of parameter values is ' num2str(length(values))]);
63 end
64 if ~isa(values, 'double')
65 error(['### Parameter ''values'' is not a double array but of class ' class(values)]);
66 end
67
68 for i_sys = 1:Nsys
69 %% getting matrix sizes
70 Nss = sys(i_sys).Nstates;
71 Ninputs = sys(i_sys).Ninputs;
72 Noutputs = sys(i_sys).Noutputs;
73 sssizes = sys(i_sys).statesizes;
74 inputsizes = sys(i_sys).inputsizes;
75 outputsizes = sys(i_sys).outputsizes;
76
77 %% setting matrix sizes
78 amats = cell(Nss*(Ndiff+1), Nss*(Ndiff+1));
79 bmats = cell(Nss*(Ndiff+1), Ninputs);
80 cmats = cell(Noutputs*(Ndiff+1), Nss*(Ndiff+1));
81 dmats = cell(Noutputs*(Ndiff+1), Ninputs);
82 sys_num = sys(i_sys).keepParameters;
83
84 %% assigning system matrices for nominal values
85 amats(1:Nss,1:Nss) = sys_num.amats;
86 bmats(1:Nss,1:Ninputs) = sys_num.bmats;
87 cmats(1:Noutputs,1:Nss) = sys_num.cmats;
88 dmats(1:Noutputs,1:Ninputs) = sys_num.dmats;
89
90 outputs = sys(i_sys).outputs;
91 states = sys(i_sys).states;
92
93 %% loop over parameters
94 for i_p = 1:Ndiff
95 % computing ssm derivative
96 sys_loc = copy(sys(i_sys), true);
97 value_loc = sys(i_sys).params.find(names{i_p}) + values(i_p);
98 sys_loc.doSetParameters(names(i_p), value_loc);
99 sys_loc.keepParameters;
100
101 % computing derivatives of matrices
102 dAmats = ssm.blockMatRecut( ( ssm.blockMatFusion(sys_loc.amats, sssizes, sssizes) - ssm.blockMatFusion(sys_num.amats, sssizes, sssizes) )/ values(i_p) , sssizes, sssizes);
103 dBmats = ssm.blockMatRecut( ( ssm.blockMatFusion(sys_loc.bmats, sssizes, inputsizes) - ssm.blockMatFusion(sys_num.bmats, sssizes, inputsizes) )/ values(i_p) , sssizes, inputsizes);
104 dCmats = ssm.blockMatRecut( ( ssm.blockMatFusion(sys_loc.cmats, outputsizes, sssizes) - ssm.blockMatFusion(sys_num.cmats, outputsizes, sssizes) )/ values(i_p) , outputsizes, sssizes);
105 dDmats = ssm.blockMatRecut( ( ssm.blockMatFusion(sys_loc.dmats, outputsizes, inputsizes) - ssm.blockMatFusion(sys_num.dmats, outputsizes, inputsizes) )/ values(i_p) , outputsizes, inputsizes);
106
107 % assigning matrices for derivatives
108 amats( (1+i_p*Nss):((i_p+1)*Nss), (1+i_p*Nss):((i_p+1)*Nss) ) = sys_num.amats;
109 amats( (1+i_p*Nss):((i_p+1)*Nss), 1:Nss ) = dAmats;
110 bmats( (1+i_p*Nss):((i_p+1)*Nss), 1:Ninputs ) = dBmats;
111 cmats( (1+i_p*Noutputs):((i_p+1)*Noutputs), (1+i_p*Nss):((i_p+1)*Nss) ) = sys_num.cmats;
112 dmats( (1+i_p*Noutputs):((i_p+1)*Noutputs), 1:Ninputs ) = dDmats;
113 cmats( (1+i_p*Noutputs):((i_p+1)*Noutputs), 1:Nss ) = dCmats;
114
115 % assigning outputs
116 outputs((1+i_p*Noutputs):((i_p+1)*Noutputs)) = sys_loc.outputs ;
117 % renaming outputs
118 for i=(1+i_p*Noutputs):((i_p+1)*Noutputs)
119 outputs(i).setBlockNames( [outputs(i).name '_DIFF_' names{i_p}] );
120 end
121
122 % assigning states
123 states((1+i_p*Nss):((i_p+1)*Nss)) = sys_loc.states ;
124 % renaming states
125 for i=(1+i_p*Nss):((i_p+1)*Nss)
126 states(i).setBlockNames( [states(i).name '_DIFF_' names{i_p}] );
127 end
128
129 clear sys_loc
130 end
131
132 %% proceeding parameters update
133 sys_out(i_sys).amats = amats;
134 sys_out(i_sys).bmats = bmats;
135 sys_out(i_sys).cmats = cmats;
136 sys_out(i_sys).dmats = dmats;
137 sys_out(i_sys).timestep = sys(i_sys).timestep;
138 sys_out(i_sys).name = sys(i_sys).name;
139 sys_out(i_sys).description = sys(i_sys).description;
140 sys_out(i_sys).params = plist;
141 sys_out(i_sys).outputs = outputs;
142 sys_out(i_sys).inputs = sys(i_sys).inputs;
143 sys_out(i_sys).states = states;
144
145 sys_out(i_sys).validate;
146
147 %% history and output arguments
148 if ~internal
149 sys_out(i_sys).addHistory(ssm.getInfo(mfilename), pl , {''}, sys(i_sys).hist );
150 end
151 end
152
153 if nargout == numel(sys_out)
154 for ii = 1:numel(sys_out)
155 varargout{ii} = sys_out(ii);
156 end
157 else
158 varargout{1} = sys_out;
159 end
160
161 end
162
163
164
165 %--------------------------------------------------------------------------
166 % Get Info Object
167 %--------------------------------------------------------------------------
168 function ii = getInfo(varargin)
169
170 if nargin == 1 && strcmpi(varargin{1}, 'None')
171 sets = {};
172 pl = [];
173 else
174 sets = {'Default'};
175 pl = getDefaultPlist;
176 end
177 % Build info object
178 ii = minfo(mfilename, 'ssm', 'ltpda', utils.const.categories.helper, '$Id: parameterDiff.m,v 1.9 2011/04/08 08:56:22 hewitson Exp $', sets, pl);
179 end
180
181 %--------------------------------------------------------------------------
182 % Get Default Plist
183 %--------------------------------------------------------------------------
184 function pl = getDefaultPlist()
185 pl = plist();
186
187 p = param({'names', 'A cell-array of parameter names for numerical differentiations.'}, {});
188 pl.append(p);
189
190 p = param({'values', 'An array of parameter values for numerical step size.'}, []);
191 pl.append(p);
192 end