comparison m-toolbox/classes/@ssm/doSubsParameters.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 % DOSUBSPARAMETERS enables to substitute symbollic patameters
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: DOSUBSPARAMETERS enables the substitution of symbolic
5 % parameters. This private method does the work of
6 % subsParameters and keepParameters.
7 %
8 % VERSION: $Id: doSubsParameters.m,v 1.10 2011/03/03 18:11:54 hewitson Exp $
9 %
10 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
11
12 function sys = doSubsParameters(sys, subsnames, callerIsMethod)
13 % checking whether any work need be done at all
14 if sys.params.nparams==0 || numel(subsnames)==0
15 return
16 end
17 sysNames = sys.params.getKeys;
18 wasFound = false(size(subsnames));
19 removeParams = false(size(sysNames));
20
21 % If we have any matrices containing symbolic parameters, then we go
22 % ahead and declare all parameters symbolic
23 if hasSymbols(sys)
24 % declaring and evaluating parameters to substitute
25 for j=1:numel(sysNames)
26 pname = sysNames{j};
27 indices = strcmpi(subsnames, pname);
28 cmd1 = [pname '=sym(''', pname, ''');'];
29
30 if sum(indices)>0
31 % remebering parameter match was found for this entry
32 wasFound(indices) = indices(indices);
33 % remembering parameter to move
34 removeParams(j) = true;
35
36 val = sys.params.params(j).getVal;
37 if isa(val, 'plist')
38 val = find(val, 'value');
39 end
40 cmd2 = [pname '=' utils.helper.num2str(val) ';'];
41 eval(cmd1);
42 eval(cmd2);
43 else
44 eval(cmd1);
45 end
46 end
47 else
48 % This means we have no symbolic matrix elements, so we can just
49 % declare local double variables. This works after you call
50 % ssm/optimiseForFitting on the model.
51 for j=1:numel(sysNames)
52 pname = sysNames{j};
53 indices = strcmpi(subsnames, pname);
54
55 if sum(indices)>0
56 % remebering parameter match was found for this entry
57 wasFound(indices) = indices(indices);
58 % remembering parameter to move
59 removeParams(j) = true;
60
61 val = sys.params.params(j).getVal;
62 if isa(val, 'plist')
63 val = find(val, 'value');
64 end
65 cmd2 = [pname '=' utils.helper.num2str(val) ';'];
66 eval(cmd2);
67 end
68 end
69
70 end
71
72
73
74 % moving substituted parameters
75 if callerIsMethod
76 % do nothing, since this implies the object will be thrown away
77 else
78 sys.numparams.append(sys.params.params(removeParams));
79 sys.params.remove(removeParams);
80 end
81
82 % warning if a parameter was not found in the symbolic parameter
83 if sum(~wasFound)>0
84 str = ['warning!! parameters : ' subsnames(~wasFound) ' were not found in ' sys.name];
85 str = char(str);
86 display(str);
87 end
88
89 % evaluation of A, B, C, D matrices
90 for i_ss=1:sys.Nss
91 % A Matrix
92 for j_ss =1:sys.Nss
93 if ~isempty(sys.amats{i_ss, j_ss}) && ~isnumeric(sys.amats{i_ss, j_ss})
94 sys.amats{i_ss, j_ss} = evalsym(sys.amats{i_ss, j_ss});
95 end
96 end
97 % B Matrix
98 for j_in =1:sys.Ninputs
99 if ~isempty(sys.bmats{i_ss, j_in}) && ~isnumeric(sys.bmats{i_ss, j_in})
100 sys.bmats{i_ss, j_in} = evalsym(sys.bmats{i_ss, j_in});
101 end
102 end
103 end
104 for i_out=1:sys.Noutputs
105 % C Matrix
106 for j_ss =1:sys.Nss
107 if ~isempty(sys.cmats{i_out, j_ss}) && ~isnumeric(sys.cmats{i_out, j_ss})
108 sys.cmats{i_out, j_ss} = evalsym(sys.cmats{i_out, j_ss});
109 end
110 end
111 % D Matrix
112 for j_in =1:sys.Ninputs
113 if ~isempty(sys.dmats{i_out, j_in}) && ~isnumeric(sys.dmats{i_out, j_in})
114 sys.dmats{i_out, j_in} = evalsym(sys.dmats{i_out, j_in});
115 end
116 end
117 end
118
119 end
120
121 function out = hasSymbols(sys)
122
123 out = false;
124 if any(cellfun('isclass', sys.amats, 'sym'))
125 out = true;
126 return;
127 end
128
129 if any(cellfun('isclass', sys.bmats, 'sym'))
130 out = true;
131 return;
132 end
133
134 if any(cellfun('isclass', sys.cmats, 'sym'))
135 out = true;
136 return;
137 end
138
139 if any(cellfun('isclass', sys.dmats, 'sym'))
140 out = true;
141 return;
142 end
143
144 end
145
146 function e = evalsym(s)
147
148 if ischar(s)
149 cmat = map2mat(s);
150 else
151 cmat = map2mat(char(s));
152 end
153
154 % '^', '*' or '/'
155 vmat = strrep(cmat, '^', '.^');
156 vmat = strrep(vmat, '*', '.*');
157 vmat = strrep(vmat, '/', './');
158 vmat(strfind(vmat,'..')) = [];
159
160 e = evalin('caller', vmat);
161
162 end
163
164 function r = map2mat(r)
165 % MAP2MAT Maple to MATLAB string conversion.
166 % MAP2MAT(r) converts the Maple string r containing
167 % matrix, vector, or array to a valid MATLAB string.
168 %
169 % Examples: map2mat(matrix([[a,b], [c,d]]) returns
170 % [a,b;c,d]
171 % map2mat(array([[a,b], [c,d]]) returns
172 % [a,b;c,d]
173 % map2mat(vector([[a,b,c,d]]) returns
174 % [a,b,c,d]
175
176 % Deblank.
177 r(strfind(r,' ')) = [];
178 % Remove matrix, vector, or array from the string.
179 r = strrep(r,'matrix([[','['); r = strrep(r,'array([[','[');
180 r = strrep(r,'vector([','['); r = strrep(r,'],[',';');
181 r = strrep(r,']])',']'); r = strrep(r,'])',']');
182 % Special case of the empty matrix or vector
183 if strcmp(r,'vector([])') || strcmp(r,'matrix([])') || ...
184 strcmp(r,'array([])')
185 r = [];
186 end
187 end