KerMor  0.9
Model order reduction for nonlinear dynamical systems and nonlinear approximation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LocalLipGradientEstPlay.m
Go to the documentation of this file.
1 namespace testing{
2 
3 
4 /* (Autoinserted by mtoc++)
5  * This source code has been filtered by the mtoc++ executable,
6  * which generates code that can be processed by the doxygen documentation tool.
7  *
8  * On the other hand, it can neither be interpreted by MATLAB, nor can it be compiled with a C++ compiler.
9  * Except for the comments, the function bodies of your M-file functions are untouched.
10  * Consequently, the FILTER_SOURCE_FILES doxygen switch (default in our Doxyfile.template) will produce
11  * attached source files that are highly readable by humans.
12  *
13  * Additionally, links in the doxygen generated documentation to the source code of functions and class members refer to
14  * the correct locations in the source code browser.
15  * However, the line numbers most likely do not correspond to the line numbers in the original MATLAB source files.
16  */
17 
19 
20 /* % Presettings */
21 
22 dt = .2;
23 area = [-20 20];
24 gamma = 15;
25 /* epsilon-threshold range */
26 epsi = .005;
27 /* rnd = false; */
28 rnd = true;
29 /* Initial current state point */
30 xp = 3;
31 yp = 2;
32 
33 /* Positions */
34 if rnd
35  kNum = 50;
36  xP = zeros(1,kNum);
37  yP = xP;
38  for cnt = 1:kNum
39  xP(cnt) = area(1) + rand * (area(2)-area(1));
40  yP(cnt) = area(1) + rand * (area(2)-area(1));
41  end
42 else
43  kNum = 4;
44  xP = [0 .2 .2 .2 8];
45  yP = [-2.5 .2 1 .5 5];
46  xP = xP(1:kNum);
47  yP = yP(1:kNum);
48 end
49 /* Coefficients */
50 if rnd
51  /* c = (rand(1,kNum)-.5)*5; */
52  c = (rand(1,kNum))*5;
53 else
54  c = [1 2 3 2 15];
55  c = c(1:kNum);
56 end
57 
58 /* % Automatic */
59 k = kernels.GaussKernel(gamma);
60 
61 /* r = area(2)-area(1); */
62 range = area(1):dt:area(2);
63 [X,Y] = meshgrid(range,range);
64 
65 /* Plot data */
66 vals = zeros(size(X,1),size(X,2),kNum);
67 for cnt = 1:kNum
68  arg = sqrt((X-xP(cnt)).^2+(Y-yP(cnt)).^2);
69  vals(:,:,cnt) = c(cnt)*k.evaluateScalar(arg);
70 end
71 Z = sum(vals,3);
72 
73 /* Distance matrix */
74 D = getDistanceMat(xP,yP);
75 /* Set large distances to zero */
76 
77 nullrad = sqrt(-gamma*log(epsi./abs(c)));
78 nullrad = repmat(nullrad,kNum,1);
79 F = k.evaluateScalar(D);
80 /* W = D./nullrad; */
81 W = 1-D./nullrad;
82 W(W<0)=epsi;
83 if ~rnd
84  nullrad
85  D
86  W
87  F
88 end
89 /* W = ones(size(D)); */
90 
91 /* Angle matrix
92  *A = getAngleMat(xP,yP) */
93 
94 /* % Postsettings */
95 lipFun = @k.getImprovedLocalSecantLipschitz;
96 
97 /* Precomputations
98  * globEst = [Inf,-Inf];
99  * globEff = [Inf,-Inf];
100  * coarsest = -Inf;
101  * sharpest = Inf;
102  * for cnt = 1:numel(X)
103  * x = X(cnt); y = Y(cnt);
104  * [estMax, di, lip] = getEstGradient(x,y);
105  * if globEst(1) > estMax
106  * globEst(1) = estMax;
107  * end
108  * if globEst(2) < estMax
109  * globEst(2) = estMax;
110  * end
111  * [effMax, xm, ym, dist] = getEffGradient(x,y);
112  * effMax = abs(effMax);
113  * if globEff(1) > effMax
114  * globEff(1) = effMax;
115  * end
116  * if globEff(2) < effMax
117  * globEff(2) = effMax;
118  * end
119  * rel = estMax/effMax;
120  * if rel > coarsest
121  * coarsest = rel;
122  * end
123  * if rel < sharpest
124  * sharpest = rel;
125  * end
126  * if mod(cnt,200) == 0
127  * fprintf('%d%%..',round(cnt/numel(X)*100));
128  * end
129  * end
130  * fprintf('\n');
131  * coarsest = round(coarsest*100);
132  * sharpest = round(sharpest*100); */
133 
134 /* Plotting */
135 h = figure;
136 cm = datacursormode(h);
137 cm.Enable= " on ";
138 cm.SnapToDataVertex= " on ";
139 cm.DisplayStyle= " window ";
140 cm.Updatefcn= @pointSelected;
141 
142 
143 subplot(1,2,1);
144 /* mesh(X,Y,Z); */
145 surf(X,Y,Z);
146 
147 shading interp
148 /* grid off; */
149 hidden off;
150 /* lighting gouraud; */
151 colormap hot;
152 axis equal;
153 hold on;
154 h = contour3(X,Y,Z,20," black ");
155 
156 lineobj = cell.empty(1,0);
157 
158 oldpos = [Inf Inf];
159 updatePlot(xp,yp);
160 
161  function updatePlot(xp,yp)
162 
163  try
164  /* % Altes zeug */
165  [estMax, di, lip] = getEstGradient(xp,yp);
166  [effMax, xm, ym, dist, Zsecant] = getEffGradient(xp,yp);
167 
168  /* % Session Jan */
169 
170  xidiff = [xP - xp; yP-yp];
171  Umat = xidiff^t*xidiff;
172  em = 0;
173  for idx=1:length(xP)
174  sameside = Umat(idx,:) > 0;
175  [estMaxLoc, didummy, lipdummy] = getEstGradientJan(xp,yp,sameside);
176  em(end+1) = abs(estMaxLoc);
177  end
178  em
179  max(em)
180  estMax
181  effMax
182 
183  /* % Altes zeug */
184 
185  [dummy, yi] = min(abs(range - xp));
186  [dummy, xi] = min(abs(range - yp));
187  /* yi = find(range == xp,1);
188  * xi = find(range == yp,1); */
189 
190  /* Gradient vectors - real */
191  for idx=1:length(lineobj)
192  lin = lineobj[idx];
193  if ~isempty(lin) && ishandle(lin)
194  delete(lin);
195  end
196  end
197  lineobj = [];
198 
199 
200 
201 
202  subplot(1,2,1);
203 
204  /* Local secant lipschitz estimations
205  * for idx = 1:kNum
206  * z1 = c(idx)*k.evaluateScalar(di(idx));
207  * z2 = z1 + c(idx)*lip(idx)*di(idx);
208  * lineobj{end+1} = plot3([xp xP(idx)],[yp yP(idx)],[z1 z2],'LineWidth',2);%#ok
209  * end */
210 
211  /* Plot single cone maxima */
212  centers = [xP; yP];
213  fxi = c*k.evaluate(centers,centers);
214  /* Plot cone with current maximum secant */
215  [v,idx] = max(abs(fxi - Z(xi,yi)) ./ sum(sqrt([xP-xp;yP-yp].^2)));
216  lineobj[end+1] = plot3(xP(idx),yP(idx),fxi(idx)," red. "," MarkerSize ",15);
217  idx = setdiff(1:length(xP),idx);
218  /* Plot other peaks black */
219  lineobj[end+1] = plot3(xP(idx),yP(idx),fxi(idx)," black. "," MarkerSize ",15);
220 
221  /* Max effective secant grad */
222  hlp = Z(xi,yi) + effMax * dist;
223  lineobj[end+1] = plot3([xp X(xm,ym)],[yp Y(xm,ym)], [Z(xi,yi) hlp]," g "," LineWidth ",2);
224 
225  /* Max computed secant grad */
226  hlp = Z(xi,yi) + sign(Z(xm,ym)-Z(xi,yi)) * estMax * dist;
227  lineobj[end+1] = plot3([xp X(xm,ym)],[yp Y(xm,ym)], [Z(xi,yi) hlp]," r "," LineWidth ",2);
228 
229  /* Estimate function secants roughly */
230  y = [xp; yp];
231  inner = di < k.x0;
232  theta = k.xR ./ di(inner);
233  t1 = repmat(theta,2,1);
234  yvec = repmat(y,1,size(centers(:,inner),2));
235  centers(:,inner) = (1-t1).*centers(:,inner) + t1.*yvec;
236  fxi = c * k.evaluate(centers,centers)^t;
237  xisec = (fxi - k.evaluateScalar(norm(y))) ./ di;
238 
239  hlp = c .* lip;
240  posi = max(hlp,0);
241  [maxp, idp] = max(xisec);
242  neg = 0;
243  idm = 1;
244  /* neg = min(hlp,0);
245  *[maxm, idm] = min(neg); */
246 
247  /* posidx = hlp >=0;
248  * negidx = hlp < 0;
249  * posnum = sum(posidx);
250  * negnum = sum(negidx);
251  * e1 = zeros(posnum,negnum);
252  * e2 = zeros(posnum,negnum);
253  * for pidx = 1:max(1,posnum)
254  * for nidx = 1:max(1,negnum)
255  * e1(pidx,nidx) = sum( (posi .* F(pidx,:) + abs(neg) .* F(nidx,:)));
256  * e2(pidx,nidx) = sum( (posi .* W(pidx,:) + abs(neg) .* W(nidx,:)));
257  * end
258  * end
259  * if all(e1(:) < abs(effMax))
260  * e1/abs(effMax)
261  * disp(round(e1/abs(effMax)*100));
262  * fprintf('e1 all too low: ');
263  * fprintf('%2.2f%%, ',round(e1/abs(effMax)*100));
264  * fprintf('\n');
265  * end
266  * if all(e2(:) < abs(effMax))
267  *disp(round(e2/abs(effMax)*100));
268  * fprintf('e2 all too low: ');
269  * fprintf('%2.2f%%, ',round(e2/abs(effMax)*100));
270  * fprintf('\n');
271  * end */
272 
273  /* Experiment 1
274  *experi = maxp + abs(maxm); */
275  experi = sum( (posi .* F(idp,:) + abs(neg) .* F(idm,:)));
276 
277  /* Plot Exp1 */
278  hlp = Z(xi,yi) + sign(Z(xm,ym)-Z(xi,yi)) * experi * dist;
279  lineobj[end+1] = plot3([xp X(xm,ym)],[yp Y(xm,ym)], [Z(xi,yi) hlp]," m "," LineWidth ",2);
280 
281  /* Experiment 2
282  *acoeff = 1-getAngle([xp; yp],xP,yP)/pi; */
283  experi2 = sum( (posi .* W(idp,:) + abs(neg) .* W(idm,:)));
284 
285  /* Plot Exp2 */
286  hlp = Z(xi,yi) + sign(Z(xm,ym)-Z(xi,yi)) * experi2 * dist;
287  lineobj[end+1] = plot3([xp X(xm,ym)],[yp Y(xm,ym)], [Z(xi,yi) hlp]," c.- "," LineWidth ",2);
288 
289 
290 
291  /* title(sprintf(['Secant estimation: %f, effective secant: %f (ratio: %3d%%)\n'...
292  * 'Global (estimated/effective): sharpest %d%%, coarsest %d%%'],estMax,abs(effMax),round(estMax/abs(effMax)*100),sharpest,coarsest)); */
293  title(sprintf([" Secant estimation: %f, effective secant: %f (ratio: %3d%%)\n "...
294  " Experimental 1: %f (ratio:%d%%), Experimental 2: %f (ratio:%d%%) "],estMax,abs(effMax),round(estMax/abs(effMax)*100),...
295  experi,round(experi/abs(effMax)*100),...
296  experi2,round(experi2/abs(effMax)*100)));
297 
298  subplot(1,2,2);
299  hold off;
300  /* Max real secant */
301  Zs = abs(Zsecant);
302  surf(X,Y,Zs)
303  shading interp;
304  hidden off;
305  colormap hot;
306  axis tight;
307  hold on;
308  h = contour3(X,Y,Zs,20," black ");
309  [dummy, maxi] = max(Zs(:));
310  plot3(X(maxi),Y(maxi),Zs(maxi)," black* "," MarkerSize ",15);
311 
312  catch ME
313  ME.getReport;
314  end
315  end
316 
317  function txt = pointSelected(hObject, eventdata)
318  info = getCursorInfo(cm);
319  pos = info.Position;
320  if ~isequal(oldpos,pos(1:2))
321  oldpos = pos(1:2);
322  updatePlot(pos(1),pos(2));
323  end
324  txt = num2str(pos);
325  end
326 
327  function [estMax, di, lip] = getEstGradient(xp,yp)
328  /* Estimations */
329  di = sqrt((xP-xp).^2 + (yP-yp).^2);
330  lip = lipFun(di,Inf,0,[]);
331  /* estimated secant gradient */
332  estMax = abs(c) * lip^t;
333  end
334 
335  function [estMax, di, lip] = getEstGradientJan(xp,yp,sameidx)
336  /* Estimations */
337  di = sqrt((xP-xp).^2 + (yP-yp).^2);
338  liptest = lipFun(di,Inf,0,[]);
339  di = di(sameidx);
340  lip = lipFun(di,Inf,0,[]);
341  /* estimated secant gradient */
342  estMax = abs(c(sameidx)) * lip^t;
343  end
344 
345  function [effMax, xm, ym, dist, sgrad] = getEffGradient(xp,yp)
346 
347  [dummy, yi] = min(abs(range - xp));
348  [dummy, xi] = min(abs(range - yp));
349  /* yi = find(range == xp),1);
350  *xi = find(range == yp),1); */
351 
352  /* Max real secant */
353  fdiff = Z - Z(xi,yi);
354  xdiff = sqrt((X-xp).^2+(Y-yp).^2);
355  sgrad = fdiff ./ (xdiff+eps);
356  /* sgrad(xi,yi) = k.evaluateD1(sqrt(xp^2+yp^2)); */
357  [val, id] = max(abs(sgrad(:)));
358  [xm,ym] = ind2sub(size(sgrad),id);
359 
360  /* Distance */
361  dist = sqrt((X(xi,yi)-X(xm,ym)).^2 + (Y(xi,yi)-Y(xm,ym)).^2);
362  /* effective secant gradient */
363  effMax = (Z(xm,ym)-Z(xi,yi)) / dist;
364  end
365 
366  function D = getDistanceMat(xP,yP)
367  /* [mx,my] = meshgrid(xP,yP);
368  *D = sqrt((mx-mx').^2+(my-my').^2); */
369  vec = [xP;yP];
370  nsq = sum(vec.^2,1);
371  n = size(vec,2);
372  D = sqrt((ones(n,1)*nsq)" + ones(n,1)*nsq - 2*(vec "*vec));
373  end
374 
375  function A = getAngleMat(xP,yP)
376  vec = [xP; yP];
377  sp = vec^t * vec;
378  no = sqrt(sum(vec.^2));
379  no = no^t*no;
380  A = acos(sp ./ no);
381  end
382 
383  function A = getAngle(v,xP,yP)
384  vec = [xP; yP];
385  sp = vec^t * v;
386  no1 = sqrt(sum(vec.^2));
387  no2 = sqrt(sum(v.^2));
388  no = no1^t*no2;
389  A = acos(sp ./ no);
390  end
391 
392 end
393 
394 
395 
396 
397 
398 
399 
400 
401 
402 }
403 
404 };
A MatLab cell array or matrix.
function LocalLipGradientEstPlay()
#define F(x, y, z)
Definition: CalcMD5.c:168
#define X(i, j)
#define Y(i, j)