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
VKOGA.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 
18 class VKOGA {
38  public: /* ( Static ) */
39 
40 
41  static function structres = VKOGA_1D_nD(integer n,logical fPGreedy,integer nG) {
42 
43  /* % Data setup */
44  x = -5:.1:5;
45  fx = [sin(x)+.2*x; cos(x)-.1*x; exp(-x.^2).*sin(x); cos(2*x).*sin(x)];
46  if nargin < 3
47  nG = 3;
48  if nargin < 2
49  fPGreedy = false;
50  if nargin < 1
51  n = 1;
52  elseif n > 2
53  n = size(fx,1);
54  end
55  end
56  end
57  fx = fx(1:n,:);
58  atd = data.ApproxTrainData(x,[],[]);
59  atd.fxi= fx;
60 
61  /* % Algorithm setup */
62  alg = approx.algorithms.VKOGA;
63  alg.MaxExpansionSize= 50;
64  alg.MaxRelErr= 1e-5;
65  alg.UsefPGreedy= fPGreedy;
66  ec = kernels.config.ExpansionConfig;
67  ec.Prototype.Kernel= kernels.GaussKernel(.8);
68  ec.StateConfig= kernels.config.GaussConfig(" G ",linspace(.5,2,nG));
69  alg.ExpConfig= ec;
70 
71  kexp = alg.computeApproximation(atd);
72 
73  /* % Plot approximated function */
74  m = length(alg.Used);
75  pm = PlotManager(false,2,2);
76  pm.LeaveOpen= true;
77 
78  h1 = pm.nextPlot(" fun "," Function "," x "," f(x) ");
79  plot(h1,x,[fx; kexp.evaluate(x)]^t);
80 
81  h2 = pm.nextPlot(" nfun "," Newton Basis Functions on training data "," x "," N(x) ");
82  plot(h2,x,alg.bestNewtonBasisValuesOnATD);
83 
84  alg.plotSummary(pm, " VKOGA Demo ");
85  pm.done;
86 
87  alg.getApproximationSummary;
88 
89  res.kexp= kexp;
90  res.atd= atd;
91  res.alg= alg;
92  }
114  static function IterationPlots(struct res,integer steps,PlotManager pm) {
115  if nargin < 3
116  pm = PlotManager(false,3,3);
117  pm.LeaveOpen= true;
118  if nargin < 2
119  steps = 9;
120  end
121  end
122 
123  if res.alg.UsefPGreedy
124  fprintf(2," Warning: f/P-Greedy selection criteria was used. Error plots are for f-Greedy case.\n ");
125  end
126  ms = 7;
127  fx = res.atd.fxi.toMemoryMatrix;
128  x = res.atd.xi.toMemoryMatrix;
129 
130  /* % Zero function plot */
131  k = [];
132  h0 = doPlot(0,zeros(size(fx)));
133 
134  /* % Iteration plots */
135  for s = 1:steps
136  k = res.kexp.getSubExpansion(s);
137  fxi = k.evaluate(x);
138  hn = doPlot(s,fxi);
139  axis(hn,axis(h0));
140  end
141 
142  if nargin < 3
143  pm.done;
144  end
145 
146  function h1 = doPlot(s,fxi)
147  h1 = pm.nextPlot(sprintf(" step%d ",s),...
148  sprintf(" Iteration %d ",s)," x "," f(x) ");
149  plot(h1,x,fx" , "LineWidth^t,2);
150  hold(h1," on ");
151 
152  err = fx-fxi;
153  allerr = Norm.L2(err);
154  plot(h1,x,allerr," r ");
155  plot(h1,x,fxi" , "--^t);
156  if ~isempty(k)
157  plot(h1,k.Centers.xi,k.evaluate(k.Centers.xi)" , "k." , "MarkerSize^t,17);
158  end
159 
160  /* Plot (next) max errors */
161  [v, idx] = max(allerr);
162  plot(h1,x(idx),v," ro "," MarkerSize ",ms);
163  [~, idx] = max(abs(err),[],2);
164  if s == 2
165  for l=1:length(idx)
166  plot(h1,[x(idx(l)) x(idx(l))+eps],[fx(l,idx(l)) fxi(l,idx(l))]," k-- ");
167  plot(h1,[x(idx(l)) x(idx(l))+eps],[fx(l,idx(l)) fxi(l,idx(l))]," kx "," MarkerSize ",ms);
168  end
169  end
170  axis(h1," tight ");
171  axis(h1,axis(h1)*1.03);
172  end
173  }
190  static function NewtonBasis_Schaback() {
191 
192  pm = PlotManager(false,2,2);
193  pm.LeaveOpen= true;
194 
195  /* get cut circle */
196  [X, ind, Xemesh, Yemesh] = cutcircle(.05);
197 
198  /* assemble training data */
199  atd = data.ApproxTrainData(X,[],[]);
200  fxi = exp(abs(X(1,:)-X(2,:)))-1;
201  atd.fxi= fxi;
202 
203  /* % Algorithm setup */
204  alg = approx.algorithms.VKOGA;
205  /* Maximal number of data sites to be finally used */
206  alg.MaxExpansionSize= 200;
207  /* tolerance for residual of function recovery */
208  alg.MaxAbsResidualErr= 1e-6;
209  /* "disable" maxrelerr check by ridiculous small amount */
210  alg.MaxRelErr= 1e-10;
211  alg.UsefPGreedy= false;
212  ec = kernels.config.ExpansionConfig;
213 
214  /* Gaussian
215  * ec.StateConfig = kernels.config.RBFConfig('G',[1 2 3]); */
216 
217  /* Wendland functions */
218  wc = kernels.config.WendlandConfig(" G ",[1 2 3]," S ",[1 1 1]);
219  wc.Dimension= 2;
220  ec.StateConfig= wc;
221 
222  alg.ExpConfig= ec;
223 
224  kexp = alg.computeApproximation(atd);
225 
226  h = pm.nextPlot(" circ "," Selected data points ");
227  plot(h,X(1,:),X(2,:)," . ",X(1,alg.Used),X(2,alg.Used)," ro ");
228 
229  Z = zeros(size(Xemesh));
230  Z(ind) = fxi;
231  h = pm.nextPlot(" fun "," Original function ");
232  surf(h,Xemesh,Yemesh,Z);
233 
234  h = pm.nextPlot(" afun "," Approximation ");
235  aZ = Z;
236  aZ(ind) = kexp.evaluate(X);
237  surf(h,Xemesh,Yemesh,aZ);
238 
239  h = pm.nextPlot(" err "," Error ");
240  surf(h,Xemesh,Yemesh,abs(Z-aZ));
241 
242  for n=1:8
243  plotBasis(n);
244  end
245  pm.done;
246 
247  function plotBasis(idx)
248  h = pm.nextPlot(sprintf(" bfun%d ",idx),sprintf(" Newton basis function %d ",idx)," x "," y ");
249  nv = zeros(size(Xemesh));
250  nv(ind) = alg.bestNewtonBasisValuesOnATD(:,idx);
251  surf(h,Xemesh,Yemesh,nv);
252  end
253 
254  function [Xe, ind, xm, ym]=cutcircle(he)
255  /* generates regular points Xe in circle with lower left quadrangle cut out
256  * The index list ind indicates the positions in meshgrid (xm,ym)
257  * when the meshgrid is written in a single point list (xee, yee) */
258  dx=2; /* space dimension */
259 
260  dy=dx;
261  /* he=0.01; % 1 D stepsize for evaluation grid.
262  * The grid can consist of very many points.
263  * he=0.01 gives N=40401 points to work on */
264  [xm, ym]=meshgrid(-1:he:1,-1:he:1);
265  xee=xm(:);
266  yee=ym(:);
267  ind=find((xee.^2+yee.^2<=1)&((xee>=0)|(yee>=0)) );
268 /* msk=zeros(size(xee));
269  * msk(ind)=1.0; */
270  Xe=[xee(ind), yee(ind)]^t;
271  end
272  }
290 };
291 }
292 
VKOGA: Contains some demo functions for the VKOGA algorithm.
Definition: VKOGA.m:18
An integer value.
PlotManager: Small class that allows the same plots generated by some script to be either organized a...
Definition: PlotManager.m:17
static function struct res = VKOGA_1D_nD(integer n,logical fPGreedy,integer nG)
Starts a demo of the approx.algorithms.VKOGA algorithm.
Definition: VKOGA.m:41
A boolean value.
#define X(i, j)
static function IterationPlots(struct res,integer steps,PlotManager pm)
Demonstrates the VKOGA iterations during approximation computations.
Definition: VKOGA.m:114
static function rowvec< double > n = L2(matrix< double > x)
Returns the discrete norm for each column vector in x.
Definition: Norm.m:39
static function NewtonBasis_Schaback()
The demo of the schaback paper for the function-dependent Newton basis.
Definition: VKOGA.m:190
Norm: Static class for commonly used norms on sets of vectors.
Definition: Norm.m:17