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
BellFunctions.m
Go to the documentation of this file.
1 namespace demos{
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 
39  public: /* ( Static ) */
40 
41  static function PlotManagerpm = SecantGradientPlots(kernels.BellFunction bfun) {
42  if nargin == 0
43  bfun = kernels.GaussKernel(2);
44  end
45 
46  r0 = bfun.r0; rm = bfun.rm; /* #ok<*PROP> */
47 
48 
49  f = @bfun.evaluateScalar;
50  df = @bfun.evaluateD1;
51 
52  lw = 1;
53  fs = 16;
54 
55  maxR = rm*2;
56  r = linspace(0,maxR,70);
57 
58  /* alls = [r0 rm+r0]/2; */
59  alls = [.2*r0 1.5*rm];
60  rs = bfun.ModifiedNewton([r0 r0],alls);
61  drs = abs(df(rs));
62 
63  pm = PlotManager;
64  pm.LeaveOpen= true;
65  pm.SaveFormats= [" eps "];
66 
67  for k=1:2
68  s = alls(k);
69  h = pm.nextPlot(sprintf(" secant_demo_%d ",k),...
70  sprintf(" Maximum secant gradient selection for bell functions, s=%f, rs=%f ",s,rs(k)));
71 
72  /* f and df */
73  plot(h,r,f(r)," r "," LineWidth ",lw); /* ,r,abs(df(r)),'m' */
74 
75  hold(h, " on ");
76 
77  /* Plot f tangent for max secant gradient */
78  plot(h,[0 maxR],f(s) + drs(k)*[s (s-maxR)]," b-- "," LineWidth ",lw);
79  /* Plot & text */
80  xoff = maxR*.001;
81  yoff = .05;
82  plot(h,[r0 r0],[0 f(r0)]," blacks "," MarkerSize ",6," MarkerFaceColor "," black ");
83  text(r0,-yoff," r_0 "," FontSize ",fs," Parent ",h);
84  text(r0+xoff,f(r0)+yoff," \phi(r_0) "," FontSize ",fs," Parent ",h);
85 
86  plot(h,[rm rm],[0 f(rm)]," bs "," MarkerSize ",6," MarkerFaceColor "," blue ");
87  text(rm,-yoff," r_m "," FontSize ",fs," Parent ",h);
88  text(rm+xoff,f(rm)+yoff," \phi(r_m) "," FontSize ",fs," Parent ",h);
89 
90  plot(h,[s s],[0 f(s)]," ro "," MarkerSize ",6," MarkerFaceColor "," red ");
91  text(s,-yoff," s "," FontSize ",fs," Parent ",h);
92  text(s+xoff,f(s)+yoff," \phi(s) "," FontSize ",fs," Parent ",h);
93 
94  plot(h,[rs(k) rs(k)],[0 f(rs(k))]," mo "," MarkerSize ",6," MarkerFaceColor "," magenta ");
95  text(rs(k),-yoff," r_s "," FontSize ",fs," Parent ",h);
96  text(rs(k)+xoff,f(rs(k))+yoff," \phi(r_s) "," FontSize ",fs," Parent ",h);
97 
98  plot(h,r,0," black ");
99 
100  axis(h,[0 maxR -.5 f(r0)+abs(df(r0))*r0]);
101 
102  lh = legend(h," \phi "," \phi(r_s) + \phi "" (r_s)(x-r_s) ");/* , '|\phi''|' */
103 
104  set(lh," FontSize ",fs);
105  end
106  }
129  static function NewtonPenalty(double Gamma) {
130  if nargin < 1
131  Gamma = 2;
132  end
133 
134  b = kernels.GaussKernel(Gamma);
135  r0 = b.r0; rm = b.rm;
136  lfun = error.lipfun.ImprovedLocalSecantLipschitz(b);
137  b.NewtonTolerance= 1e-3;
138  lfun.prepareConstants;
139 
140  plotrs = false;
141  lw = 1;
142 
143  maxR = Gamma*3;
144  r = linspace(0,maxR,70);
145  r = union(r,[r0,rm]);
146 
147  rstart = repmat(r0/2,1,size(r,2));
148  rss = b.ModifiedNewton(rstart,r);
149 
150  f = @b.evaluateScalar;
151  df = @b.evaluateD1;
152 
153  /* kernels.BellFunction.showNewton(x,bell.x0,y,f,df,ddf); */
154  fh = figure;
155  m = length(r);
156  for k=1:m
157  s = r(k);
158 
159  /* Get precomputed values */
160  drf = abs(df(rss));
161 
162  /* kernels.BellFunction.showNewton(x,bell.x0,y,f,df,ddf); */
163 
164  /* Add eps at division as for zero nominator the expression is zero. (no error made) */
165  n(1,1:m) = b.evaluateD1(0) - (b.evaluateScalar(0)-b.evaluateScalar(s))./(eps-s);
166  n(2,1:m) = b.evaluateD1(r0) - (b.evaluateScalar(r0)-b.evaluateScalar(s))./(b.r0-s+eps);
167  n(3,1:m) = b.evaluateD1(rm) - (b.evaluateScalar(rm)-b.evaluateScalar(s))./(b.rm-s+eps);
168 
169  /* Lim x->y n'(x) = \phi''(x) / 2 (De L'hospital) */
170  dn(1,1:m) = b.evaluateD2(0) - n(1,:)./-s;
171  dn(1,isnan(dn(1,:))) = b.evaluateD2(0)/2;
172  dn(2,1:m) = b.evaluateD2(r0) - n(2,:)./(r0-s);
173  dn(2,isinf(dn(2,:))) = 0; /* == ddf(x0)/2; */
174 
175  dn(3,1:m) = b.evaluateD2(rm) - n(3,:)./(rm-s);
176  dn(3,isinf(dn(3,:))) = b.evaluateD2(rm)/2;
177 
178  /* Get optimization function */
179  [opt,~,inner,l0,gxr] = b.optFun(r,repmat(s,1,size(r,2)),n,dn);
180  std = ~inner & ~l0 & ~gxr;
181 
182  /* f and df */
183  plot(r,f(r)," r ",r,abs(df(r))," m "," LineWidth ",lw);
184  st=[];
185  st(1:2) = [" \phi ", " |\phi "" | "]; /* #ok<*AGROW> */
186 
187  hold on;
188 
189  /* Plot abs(df(rs)) for all s */
190  if plotrs
191  plot(r,abs(drf)," g "," LineWidth ",lw);
192  st[end+1] = " |\phi "" (r_s)| = |S_s(r_s))| \forall s ";
193  end
194  /* Plot f tangent for max secant gradient */
195  plot([0 maxR],f(s) + drf(k)*[s (s-maxR)]," k-- "," LineWidth ",lw);
196  st[end+1] = " \phi(r_s) + \phi "" (r_s)(x-r_s) ";
197  /* Plot */
198  if any(std)
199  plot(r(std),opt(std)," b "," LineWidth ",lw);
200  st[end+1] = " n(r) ";
201  end
202  if any(inner)
203  plot(r(inner),opt(inner)," b-- "," LineWidth ",lw);
204  st[end+1] = " c(r_0)(r+d(r_0))^2 ";
205  end
206  if any(l0)
207  plot(r(l0),opt(l0)," b-. "," LineWidth ",lw);
208  st[end+1] = " c(0)(r+d(0))^2 ";
209  end
210  if (any(gxr))
211  plot(r(gxr),opt(gxr)," b-. "," LineWidth ",lw);
212  st[end+1] = " c(r_m)(r+d(r_m))^2 ";
213  end
214  plot([rss(k) rss(k)],[f(rss(k)) abs(drf(k))]," --black "," LineWidth ",lw);
215  if plotrs
216  plot([rss(k) s],[abs(drf(k)) abs(drf(k))]," --black "," LineWidth ",lw);
217  end
218  st[end+1] = " \phi(r_s) <-> d\phi(rs) = |S_s(r_s)| ";
219 
220  fs = 16;
221  off = .04; off2 = .07;
222  plot([r0 r0],[0 f(r0)]," blacks "," MarkerSize ",6," MarkerFaceColor "," black ");
223  text(r0+off,f(r0)+off," \phi(r_0) "," FontSize ",fs);
224  text(r0,-off2," r_0 "," FontSize ",fs);
225  plot([rm rm],[0 f(rm)]," bs "," MarkerSize ",6," MarkerFaceColor "," blue ");
226  text(rm+off,f(rm)+off," \phi(r_m) "," FontSize ",fs);
227  text(rm,-off2," r_m "," FontSize ",fs);
228  plot([s s],[0 f(s)]," ro "," MarkerSize ",6," MarkerFaceColor "," red ");
229  text(s+off,f(s)+off," \phi(s) "," FontSize ",fs);
230  text(s,-off2," s "," FontSize ",fs);
231  plot([rss(k) rss(k)],[0 f(rss(k))]," mo "," MarkerSize ",6," MarkerFaceColor "," magenta ");
232  text(rss(k)+off,f(rss(k))+off," \phi(r_s) "," FontSize ",fs);
233  text(rss(k),-off2," r_s "," FontSize ",fs);
234  /* st(end+1:end+4) = {'\phi(r_0)','\phi(r_m)','\phi(s)','\phi(r_s)'}; */
235 
236  /* Plot line through zero */
237  plot(r,0," black ");
238 
239  title(sprintf(" Maximum secant gradient r_s=%g for s=%g ",rss(k),s));
240 
241  axis([0 maxR -.5 f(r0)+abs(df(r0))*r0]);
242  hold off;
243 
244  legend(st[:]);
245  pause(1);
246  /* Stop if figure has been closed */
247  if ~ishandle(fh)
248  return;
249  end
250  end
251  }
262  static function LocalLipschitzDemo(x0,C) {
263 
264  h = figure(1);
265  dt = 0.1;
266  b = 1;
267 
268  if nargin < 2
269  Cint = .1:.5:3;
270  /* Cint = 2; */
271  if nargin < 1
272  x0 = sqrt(b/2)+2;
273  end
274  else
275  Cint = C;
276  end
277  Cint(end+1) = Inf;
278 
279  x = 0:dt:8.5;
280  x = union(x,x0);
281 
282  bfun = kernels.GaussKernel(b);
283 
284  f = @(x)bfun.evaluateScalar(x);
285  df = @(x)bfun.evaluateD1(x);
286  ddf = @(x)bfun.evaluateD2(x);
287 
288  fx = f(x);
289  maxR = bfun.r0;
290 
291  /* precompute xfeats-vector
292  *xfeats = newton(maxR+sign(maxR-x),x,f,df,ddf,...
293  * K.NewtonTolerance,K.r0,K.PenaltyFactor); */
294 
295  for C = Cint
296  maxder = ones(size(x))*abs(df(bfun.r0));
297  LGL = maxder;
298  LSE = maxder;
299  minder = zeros(size(x));
300  maxd = zeros(size(x));
301  for i = 1:length(x)
302  d = abs(x(i));
303 
304  /* Less efficient for non-secant case, but shows
305  * advantage of estimation on [maxR, xfeat] more nicely */
306  if d - C - maxR > 0
307  LGL(i) = abs(df(d-C));
308  LSE(i) = (f(d-C) - f(d)) / C;
309  elseif d + C - maxR < 0
310  LGL(i) = abs(df(d+C));
311  LSE(i) = (f(d) - f(d+C)) / C;
312  else
313  xfeat = bfun.ModifiedNewton(bfun.r0/2,d);
314  if d + C - xfeat < 0
315  LSE(i) = (f(d) - f(d+C)) / C;
316  elseif d - C - xfeat > 0
317  LSE(i) = (f(d-C) - f(d)) / C;
318  else
319  LSE(i) = abs(df(xfeat));
320  end
321  end
322 
323 
324  /* More efficient method (better GLE)
325  * xfeat = maxR;
326  * if abs(d-maxR) < C
327  * xfeat = bfun.ModifiedNewton(bfun.r0/2,d);
328  * end
329  * if d + C - xfeat < 0
330  * LGL(i) = abs(df(d+C));
331  * LSE(i) = (f(d) - f(d+C)) / C;
332  * elseif d - C - xfeat > 0
333  * LGL(i) = abs(df(d-C));
334  * LSE(i) = (f(d-C) - f(d)) / C;
335  * else
336  * LSE(i) = abs(df(xfeat));
337  * end */
338 
339 
340  /* Minimum possible derivative */
341  di = abs(x - x(i));
342  idx = di <= C;
343  minder(i) = max(abs(fx(idx) - exp(-x(i).^2/b)) ./ di(idx));
344 
345  /* maxD-Comp */
346  d = (x(i)-x0);
347  maxd(i) = (fx(i)-exp(-x0.^2/b))/d;
348 
349  end
350 
351  ph = plot(x,f(x)," r ",x,abs(df(x))," r-- ",x,LGL," b ",x,LSE," g ");
352  set(ph," LineWidth ",2);
353  hold on;
354  title(sprintf(" C=%f ",C));
355 
356  skip = round(1/(3*dt));
357  plot(x(1:skip:end),minder(1:skip:end)," m^ "," MarkerSize ",6);
358  legend(" Gaussian "," Abs. Gaussian derivative ",...
359  " Local Gradient estimate (LGL) "," Local secant estimate (LSL) ",...
360  " Minimal Lipschitz constant ");
361 
362  /* C-range plot */
363  plot([x0-C x0-C+eps], [0 1], " black-- ",[x0 x0+eps], [0 1], " black ",[x0+C x0+C+eps], [0 1], " black-- ");
364 
365  /* maxR-Stelle (sqrt(b/2)) */
366  plot(x,0," black ");
367  plot(maxR,bfun.evaluateScalar(maxR)," r. "," MarkerSize ",15);
368 
369  /* maxderivative at x0-Plot
370  * plot(x,maxd,'black');
371  * plot(x,max(maxd),'black'); */
372 
373  /* x0idx = find(x==x0,1);
374  * stx0 = -e3(x0idx);
375  * stxfeat = df(xfeats(x0idx));
376  * x0feat = xfeats(x0idx);
377  * xfeats-gerade
378  * plot([0 x0 5],[-x0*stxfeat+f(x0) f(x0) (5-x0)*stxfeat+f(x0)],'g--',x0,abs(stxfeat),'green.');
379  * plot([0 x0 5],[-x0*stx0+f(x0) f(x0) (5-x0)*stx0+f(x0)],'magenta--',x0,abs(stxfeat),'magenta.');
380  * plot([x0feat x0feat+eps],[0 1],'y');
381  *plot([x0 xfeats(x0idx)],[f(x0) sign(maxR-x0)*(x0-x0feat)*stx0+f(x0)],'r',x0,stx0,'blackx'); */
382 
383 
384  axis tight;
385  hold off;
386  pause;
387  if ~ishandle(h)
388  return;
389  end
390  end
391  close(h);
392  }
401 };
402 }
403 
function rtmp = ModifiedNewton(rstart, s)
Definition: BellFunction.m:170
static function PlotManager pm = SecantGradientPlots(kernels.BellFunction bfun)
Produces the demo images for maximum secant gradient positions.
Definition: BellFunctions.m:41
BELLFUNCTION Summary of this class goes here Detailed explanation goes here.
Definition: BellFunction.m:18
static function NewtonPenalty(double Gamma)
Demonstrates the error estimator penalized newton function and maximum secant gradients.
rm
The maximum ("right") value for any .
Definition: BellFunction.m:109
PlotManager: Small class that allows the same plots generated by some script to be either organized a...
Definition: PlotManager.m:17
logical LeaveOpen
Flag indicating if the plots should be left open once the PlotManager is deleted (as variable) ...
Definition: PlotManager.m:213
static function LocalLipschitzDemo(x0, C)
ERRORESTDEMO Demo for the monotone radial basis functions error estimator.
r0
Point of maximum first derivative on scalar evaluation.
Definition: BellFunction.m:61
virtual function phir = evaluateScalar(matrix< double > r)
Allows the evaluation of the function for scalar directly.
BellFunctions: Demos regarding the Bell function local Lipschitz estimations.
Definition: BellFunctions.m:18
virtual function dr = evaluateD1()
Method for first derivative evaluation.