comparison m-toolbox/classes/@ssm/steadyState.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 % STEADYSTATE returns a possible value for the steady state of an ssm.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: STEADYSTATE returns a possible value for the steady state
5 % of the state space of an ssm with given inputs.
6 %
7 % CALL: [pl_out] = steadyState(sys, pl)
8 %
9 % INPUTS:
10 % - sys, an ssm object
11 %
12 % OUTPUTS:
13 % _ pl_out contains 'state', the random state position
14 %
15 % <a href="matlab:utils.helper.displayMethodInfo('ssm', 'steadyState')">Parameters Description</a>
16 %
17 % VERSION: $Id: steadyState.m,v 1.11 2011/04/08 08:56:23 hewitson Exp $
18 %
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
20
21 % TO DO: Check input aos for the timestep, tsdata, and ssm.timestep
22 % options to be defined (NL case)
23 % add check if one input mach no ssm input variable
24 % allow use of other LTPDA functions to generate white noise
25
26
27 function varargout = steadyState(varargin)
28
29 %% starting initial checks
30
31 % Check if this is a call for parameters
32 if utils.helper.isinfocall(varargin{:})
33 varargout{1} = getInfo(varargin{3});
34 return
35 end
36
37 utils.helper.msg(utils.const.msg.MNAME, ['running ', mfilename]);
38
39 % Collect input variable names
40 in_names = cell(size(varargin));
41 for ii = 1:nargin,in_names{ii} = inputname(ii);end
42
43 % Collect all SSMs and plists
44 [sys, ssm_invars, rest] = utils.helper.collect_objects(varargin(:), 'ssm', in_names);
45 [pl, invars2, rest] = utils.helper.collect_objects(rest(:), 'plist');
46 if ~isempty(rest)
47 pl = combine(pl, plist(rest{:}));
48 end
49 pl = combine(pl, getDefaultPlist());
50
51 %% begin function body
52
53 if numel(sys)~=1
54 error('simulate needs exactly one ssm as an input')
55 end
56 if ~sys.isnumerical
57 error(['error because system ',sys.name,' is not numerical']);
58 end
59 timestep = sys.timestep;
60 if timestep==0
61 error('timestep should not be 0 in steadyState!!')
62 end
63 if pl.isparam('noise variable names')
64 error('The noise option used must be split between "covariance" and "cpsd". "noise variable names" does not exist anymore!')
65 end
66 sssizes = sys.sssizes;
67 %% collecting simulation i/o data
68
69 constants_in = find(pl, 'constants');
70 cov_in = find(pl, 'covariance');
71 cpsd_in = find(pl, 'CPSD');
72 noise_in = blkdiag(cov_in, cpsd_in/(timestep*2));
73 [U1,S1,V1] = svd(noise_in.');
74 if (sum(S1<0)>0)
75 error('Covariance matrix is not positive definite')
76 end
77 noise_mat = U1*sqrt(S1);
78
79 %% modifying system's ordering
80 if find(pl, 'reorganize')
81 sys = reorganize(sys, pl, 'set', 'for simulate', 'internal', 'internal');
82 end
83
84 %% getting system's i/o sizes
85 inputSizes = sys.inputsizes;
86
87 Nnoise = inputSizes(2);
88 Nconstants = inputSizes(3);
89
90 if numel(diag(noise_in))~=Nnoise
91 error(['There are ' num2str(numel(diag(noise_in))) ' input noise variances and ' num2str(Nnoise) ' corresponding inputs indexed.' ])
92 elseif numel(constants_in)~=Nconstants
93 error(['There are ' num2str(numel(constants_in)) ' input constants and ' num2str(Nconstants) ' corresponding inputs indexed.' ])
94 end
95
96 A = sys.amats{1,1};
97 Bnoise = sys.bmats{1,2} * noise_mat;
98 Bcst = sys.bmats{1,3} * reshape(constants_in, Nconstants, 1);
99
100 %% counting powers of 2 to use for initilization
101 nSteps = 500;
102 tSteady = find(pl, 'tSteady');
103 nPow2 = nextpow2(tSteady/(nSteps*timestep));
104
105 %% simulation loop
106 A_pow2=cell(1,nPow2);
107 G_pow2=cell(1,nPow2);
108
109 A_pow2{1} = A;
110 G_pow2{1} = Bcst;
111
112 %% method 1 : iterate equations with growing time-step for a very long time
113 E_pow2=cell(1,nPow2);
114 E_pow2{1} = Bnoise;
115 for i_pow2 = 2:nPow2
116 G_pow2{i_pow2} = G_pow2{i_pow2-1} + A_pow2{i_pow2-1}*G_pow2{i_pow2-1};
117 E_pow2{i_pow2} = E_pow2{i_pow2-1} + A_pow2{i_pow2-1}*E_pow2{i_pow2-1};
118 A_pow2{i_pow2} = A_pow2{i_pow2-1}^2;
119 end
120 lastX = zeros(size(A,1),1);
121 for i_pow2 = fliplr(1:nPow2)
122 A = A_pow2{i_pow2};
123 G = G_pow2{i_pow2};
124 E = E_pow2{i_pow2};
125 noise_array = randn(size(E,2), nSteps);
126 for i_steps = 1:nSteps
127 lastX = A*lastX + G + E*noise_array(:,i_steps) ;
128 end
129 end
130
131 %% method 2 : compute the limit state-mean and covariance as i_pow2 tends to infinity
132 % P_pow2=cell(1,nPow2);
133 % P_pow2{1} = Bnoise*Bnoise.';
134 % for i_pow2 = 2:nPow2
135 % G_pow2{i_pow2} = G_pow2{i_pow2-1} + A_pow2{i_pow2-1}*G_pow2{i_pow2-1}; % taking step response to 2 longer time;
136 % P_pow2{i_pow2} = P_pow2{i_pow2-1} + A_pow2{i_pow2-1}*P_pow2{i_pow2-1}*(A_pow2{i_pow2-1}.');% taking state covariance to 2 longer time;
137 % A_pow2{i_pow2} = A_pow2{i_pow2-1}^2;
138 % end
139 % [U1,S1,V1] = svd(P_pow2{nPow2});
140 % lastX = U1*sqrt(S1)*randn(size(A,1),1) + G_pow2{nPow2};
141
142 %% construct output analysis object
143 plist_out = plist('state', ssm.blockMatRecut(lastX,sssizes,1) );
144 varargout = {plist_out};
145 end
146
147
148 %--------------------------------------------------------------------------
149 % Get Info Object
150 %--------------------------------------------------------------------------
151 function ii = getInfo(varargin)
152
153 if nargin == 1 && strcmpi(varargin{1}, 'None')
154 sets = {};
155 pl = [];
156 else
157 sets = {'Default'};
158 pl = getDefaultPlist;
159 end
160 % Build info object
161 ii = minfo(mfilename, 'ssm', 'ltpda', utils.const.categories.op, '$Id: steadyState.m,v 1.11 2011/04/08 08:56:23 hewitson Exp $', sets, pl);
162 end
163
164 %--------------------------------------------------------------------------
165 % Get Default Plist
166 %--------------------------------------------------------------------------
167 function pl = getDefaultPlist()
168 pl = plist();
169
170 p = param({'cpsd variable names', 'A cell-array of strings specifying the desired input variable names.'}, {} );
171 pl.append(p);
172
173 p = param({'cpsd', 'The covariance of this noise between input ports for the <i>time-continuous</i> noise model.'}, []);
174 pl.append(p);
175
176 p = param({'covariance variable names', 'A cell-array of strings specifying the desired input variable names.'}, {} );
177 pl.append(p);
178
179 p = param({'covariance', 'The covariance of this noise between input ports for the <i>time-continuous</i> noise model.'}, []);
180 pl.append(p);
181
182 p = param({'constant variable names', 'A cell-array of strings of the desired input variable names.'}, {});
183 pl.append(p);
184
185 p = param({'constants', 'Array of DC values for the different corresponding inputs.'}, paramValue.DOUBLE_VALUE(zeros(0,1)));
186 pl.append(p);
187
188 p = param({'tSteady', 'The settling time used in the calculation, in the same unit as the ssm''s timestep'}, paramValue.DOUBLE_VALUE(10^6) );
189 pl.append(p);
190
191 p = param({'reorganize', 'When set to 0, this means the ssm does not need be modified to match the requested i/o. Faster but dangerous!'}, paramValue.TRUE_FALSE);
192 pl.append(p);
193
194 end
195