-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlti2polysys.m
213 lines (190 loc) · 8.64 KB
/
lti2polysys.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
function polysys = lti2polysys(strings,y,u,varargin)
% polysys = lti2polysys(strings,y,u,'na',na,'nb',nb,...)
% ------------------------------------------------------
%
% Converts a cell of strings, representing a polynomial system from a PEM
% of a LTI model, into a polysys cell.
%
% polysys = cell containing coefficients and monomials exponents of the
% set of polynomial equations.
%
% strings = cell, each cell contains 1 string from a LTI model
%
% y = vector, contains the measured output for system
% identification
%
% u = vector, contains the measured input for system
% identification
%
% na = scalar, optional: number of 'a' parameters, other optional
% parameters are nb, nc, nd, nf, nl (lagrange multipliers)
% and ne (prediction errors).
%
% CALLS
% -----
%
% Kim Batselier, 2011-10
na=0;
nb=0;
nc=0;
nd=0;
nf=0;
nl=0;
ne=0;
for k = 1 : 2 : length(varargin)
if varargin{k} == 'na'
na = varargin{k+1};
elseif varargin{k} == 'nb'
nb = varargin{k+1};
elseif varargin{k} == 'nc'
nc = varargin{k+1};
elseif varargin{k} == 'nc'
nd = varargin{k+1};
elseif varargin{k} == 'nf'
nf = varargin{k+1};
elseif varargin{k} == 'nl'
nl = varargin{k+1};
elseif varargin{k} == 'ne'
ne = varargin{k+1};
end
end
n = na+nb+nc+nd+nf+nl+ne; % number of unknowns
polysys = cell(length(strings),2);
for stringcounter = 1 : length(strings) % process each string
temp = strings{stringcounter};
l = length(temp);
pindex = strfind(temp,'+'); % find the +'s
mindex = strfind(temp,'-'); % find the -'s
indices = sort([pindex mindex]); %borders of the terms
if isempty(indices)
indices = [1];
elseif indices(1) ~= 1
% first term does not start with '-'
indices = [1 indices];
end
indices = [indices l+1];
coefs = zeros(1,length(indices)-1);
polysys{stringcounter,2} = [];
for j = 1 : length(indices)-1 % for each term
term = temp(indices(j):indices(j+1)-1); % isolate the term
% process the coefficients
prodindex = [0 strfind(term,'*') length(term)+1];
tempcoef = '1';
exp = zeros(1,n);
for k = 1 : length(prodindex)-1 % each factor
factor = term(prodindex(k)+1:prodindex(k+1)-1);
checkindex = 1; % default index for k ~= 1
signchar = '';
if factor(1) == '-'
checkindex = 2; % first char is a sign, start to check from second
tempcoef = [tempcoef '*(-1)'];
elseif factor(1) == '+'
checkindex = 2; % first char is a sign, start to check from second
end
% if factor(1) == '-'
switch factor(checkindex)
case 'u'
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
expind = strfind(factor,'^');
expvalue = '1';
if ~isempty(expind)
expvalue = factor(expind+1:end);
end
tempcoef = [tempcoef '* u(' factor(blindex+1:brindex-1) ')^' expvalue];
case 'y'
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
expind = strfind(factor,'^');
expvalue = '1';
if ~isempty(expind)
expvalue = factor(expind+1:end);
end
tempcoef = [tempcoef '* y(' factor(blindex+1:brindex-1) ')^' expvalue];
case 'a'
%find out which a
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
aindex = str2num(factor(blindex+1:brindex-1));
% check for exponent
expind = strfind(factor,'^');
expvalue = 1;
if ~isempty(expind)
expvalue = str2num(factor(expind+1:end));
end
exp = exp + [zeros(1,aindex-1) expvalue zeros(1,n-aindex)];
case 'b'
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
bindex = str2num(factor(blindex+1:brindex-1));
% check for exponent
expind = strfind(factor,'^');
expvalue = 1;
if ~isempty(expind)
expvalue = str2num(factor(expind+1:end));
end
exp = exp + [zeros(1,na) zeros(1,bindex-1) expvalue zeros(1,n-na-bindex)];
case 'c'
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
cindex = str2num(factor(blindex+1:brindex-1));
% check for exponent
expind = strfind(factor,'^');
expvalue = 1;
if ~isempty(expind)
expvalue = str2num(factor(expind+1:end));
end
exp = exp + [zeros(1,na+nb) zeros(1,cindex-1) expvalue zeros(1,n-na-nb-cindex)];
case 'd'
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
dindex = str2num(factor(blindex+1:brindex-1));
% check for exponent
expind = strfind(factor,'^');
expvalue = 1;
if ~isempty(expind) % exponent bigger than 1
expvalue = str2num(factor(expind+1:end));
end
exp = exp + [zeros(1,na+nb+nc) zeros(1,dindex-1) expvalue zeros(1,n-na-nb-nc-dindex)];
case 'f'
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
findex = str2num(factor(blindex+1:brindex-1));
% check for exponent
expind = strfind(factor,'^');
expvalue = 1;
if ~isempty(expind) % exponent bigger than 1
expvalue = str2num(factor(expind+1:end));
end
exp = exp + [zeros(1,na+nb+nc+nd) zeros(1,findex-1) expvalue zeros(1,n-na-nb-nc-nd-findex)];
case 'l'
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
lindex = str2num(factor(blindex+1:brindex-1));
% check for exponent
expind = strfind(factor,'^');
expvalue = 1;
if ~isempty(expind) % exponent bigger than 1
expvalue = str2num(factor(expind+1));
end
exp = exp + [zeros(1,na+nb+nc+nd+nf) zeros(1,lindex-1) expvalue zeros(1,n-na-nb-nc-nd-nf-lindex)];
case 'e'
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
eindex = str2num(factor(blindex+1:brindex-1));
% check for exponent
expind = strfind(factor,'^');
expvalue = 1;
if ~isempty(expind) % exponent bigger than 1
expvalue = str2num(factor(expind+1));
end
exp = exp + [zeros(1,na+nb+nc+nd+nf+nl) zeros(1,eindex-1) expvalue zeros(1,n-na-nb-nc-nd-nf-nl-eindex)];
otherwise
tempcoef = [tempcoef '* ' '(' factor(checkindex:end) ')' ];
end
end
coefs(j)= eval(tempcoef);
polysys{stringcounter,2} = [polysys{stringcounter,2};exp];
end
polysys{stringcounter,1} = coefs;
end