-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathevalV.m
205 lines (189 loc) · 7.41 KB
/
evalV.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
function V = evalV(Vstring,y,u,roots,varargin)
% V = evalV(Vstring,y,u,roots,varargin)
% --------------------------------------
%
% Evaluates the given cost function, given as the string Vstring, in the
% roots of the polynomial system.
%
% V = vector, contains the evaluated cost function
%
% y = vector, contains the measured output for system
% identification
%
% u = vector, contains the measured input for system
% identification
%
% roots = matrix, each column corresponds with a solution of the
% polynomial system
%
% 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 i = 1 : 2 : length(varargin)
if varargin{i} == 'na'
na = varargin{i+1};
elseif varargin{i} == 'nb'
nb = varargin{i+1};
elseif varargin{i} == 'nc'
nc = varargin{i+1};
elseif varargin{i} == 'nc'
nd = varargin{i+1};
elseif varargin{i} == 'nf'
nf = varargin{i+1};
elseif varargin{i} == 'nl'
nl = varargin{i+1};
elseif varargin{i} == 'ne'
ne = varargin{i+1};
end
end
% roots zijn rijen van eerste graad uit kernel
[n nRoots] = size(roots);
V = zeros(1,nRoots);
l = length(Vstring);
pindex = strfind(Vstring,'+'); % find the +'s
mindex = strfind(Vstring,'-'); % find the -'s
indices = sort([pindex mindex]); %borders of the terms
if indices(1) ~= 1
% first term does not start with '-'
indices = [1 indices];
end
indices = [indices l+1];
for j = 1 : length(indices)-1 % for each term
term = Vstring(indices(j):indices(j+1)-1);
prodindex = [0 strfind(term,'*') length(term)+1];
tempcoef = '1';
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
switch factor(checkindex)
case 'u'
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
% check for exponent
expind = strfind(factor,'^');
expvalue = 1;
if ~isempty(expind)
expvalue = str2num(factor(expind+1:end));
end
tempcoef = [tempcoef '.* u(' factor(blindex+1:brindex-1) ')^' num2str(expvalue)];
case 'y'
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
expind = strfind(factor,'^');
expvalue = 1;
if ~isempty(expind)
expvalue = str2num(factor(expind+1:end));
end
tempcoef = [tempcoef '.* y(' factor(blindex+1:brindex-1) ')^' num2str(expvalue)];
case 'a'
%find out which a
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
aindex = str2num(factor(blindex+1:brindex-1));
rowindex = aindex;
% check for exponent
expind = strfind(factor,'^');
expvalue = 1;
if ~isempty(expind)
expvalue = str2num(factor(expind+1:end));
end
tempcoef = [tempcoef '.* roots(' num2str(rowindex) ',:).^' num2str(expvalue)];
case 'b'
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
bindex = str2num(factor(blindex+1:brindex-1));
rowindex = na+bindex;
% check for exponent
expind = strfind(factor,'^');
expvalue = 1;
if ~isempty(expind)
expvalue = str2num(factor(expind+1:end));
end
tempcoef = [tempcoef '.* roots(' num2str(rowindex) ',:).^' num2str(expvalue)];
case 'c'
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
cindex = str2num(factor(blindex+1:brindex-1));
rowindex = na+nb+cindex;
% check for exponent
expind = strfind(factor,'^');
expvalue = 1;
if ~isempty(expind)
expvalue = str2num(factor(expind+1:end));
end
tempcoef = [tempcoef '.* roots(' num2str(rowindex) ',:).^' num2str(expvalue)];
case 'd'
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
dindex = str2num(factor(blindex+1:brindex-1));
rowindex = na+nb+nc+dindex;
% check for exponent
expind = strfind(factor,'^');
expvalue = 1;
if ~isempty(expind) % exponent bigger than 1
expvalue = str2num(factor(expind+1:end));
end
tempcoef = [tempcoef '.* roots(' num2str(rowindex) ',:).^' num2str(expvalue)];
case 'f'
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
findex = str2num(factor(blindex+1:brindex-1));
rowindex = na+nb+nc+nd+findex;
% check for exponent
expind = strfind(factor,'^');
expvalue = 1;
if ~isempty(expind) % exponent bigger than 1
expvalue = str2num(factor(expind+1:end));
end
tempcoef = [tempcoef '.* roots(' num2str(rowindex) ',:).^' num2str(expvalue)];
case 'l'
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
lindex = str2num(factor(blindex+1:brindex-1));
rowindex = na+nb+nc+nd+nf+lindex;
% check for exponent
expind = strfind(factor,'^');
expvalue = 1;
if ~isempty(expind) % exponent bigger than 1
expvalue = str2num(factor(expind+1));
end
tempcoef = [tempcoef '.* roots(' num2str(rowindex) ',:).^' num2str(expvalue)];
case 'e'
blindex = strfind(factor,'[');
brindex = strfind(factor,']');
eindex = str2num(factor(blindex+1:brindex-1));
rowindex = na+nb+nc+nd+nf+nl+eindex;
% check for exponent
expind = strfind(factor,'^');
expvalue = 1;
if ~isempty(expind) % exponent bigger than 1
expvalue = str2num(factor(expind+1));
end
tempcoef = [tempcoef '.* roots(' num2str(rowindex) ',:).^' num2str(expvalue)];
otherwise
tempcoef = [tempcoef '.* ' '(' factor(checkindex:end) ')' ];
end
end
% update V
V = V + eval(tempcoef);
end
end