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
KernelInterpol.m
Go to the documentation of this file.
1 namespace general{
2 namespace interpolation{
3 
4 
5 /* (Autoinserted by mtoc++)
6  * This source code has been filtered by the mtoc++ executable,
7  * which generates code that can be processed by the doxygen documentation tool.
8  *
9  * On the other hand, it can neither be interpreted by MATLAB, nor can it be compiled with a C++ compiler.
10  * Except for the comments, the function bodies of your M-file functions are untouched.
11  * Consequently, the FILTER_SOURCE_FILES doxygen switch (default in our Doxyfile.template) will produce
12  * attached source files that are highly readable by humans.
13  *
14  * Additionally, links in the doxygen generated documentation to the source code of functions and class members refer to
15  * the correct locations in the source code browser.
16  * However, the line numbers most likely do not correspond to the line numbers in the original MATLAB source files.
17  */
18 
20  :public KerMorObject,
21  public IKernelCoeffComp {
69  public: /* ( setObservable ) */
70 
91  double RelTol = 1e-13;
118  private:
119 
120  data.FileMatrix K;
132  private:
133 
134  kexp;
142  public:
143 
144 
146  this = this@KerMorObject;
147  this.MultiTargetComputation= true;
148  this.registerProps(" UseNewtonBasis ");
149  }
150 
151 
152  function copy = clone() {
153  copy = general.interpolation.KernelInterpol;
154  copy = clone@IKernelCoeffComp(this, copy);
155  copy.UseNewtonBasis= this.UseNewtonBasis;
156  copy.K= this.K;
157  copy.kexp= this.kexp;
158  }
159 
160 
161  function [matrix<double>ai , matrix<double>nbase , usedcenteridx ] = interpolate(fxi) {
162  N = size(fxi,2);
163  if this.UseNewtonBasis
164  NV = zeros(N,N);
165  c = zeros(size(fxi,1),N);
166  fxin = Norm.L2(fxi);
167  sumNsq = zeros(1,N);
168  fresidual = fxi;
169  usedcenteridx = zeros(1,N);
170  for m = 1:N
171  res = sum(fresidual.^2,1);
172  [~, maxidx] = max(res);
173  col = this.kexp.getKernelMatrixColumn(maxidx);
174  tN = col - NV(:,1:m-1)*NV(maxidx,1:m-1)^t;
175  tNnorm = sqrt(col(maxidx)-sumNsq(maxidx));
176  if max(res./fxin) < this.RelTol || ~isreal(tNnorm) || tNnorm <= 0
177  m = m-1;/* #ok */
178 
179  break;
180  end
181  NV(:,m) = tN/tNnorm;
182  c(:,m) = fresidual(:,maxidx)./tNnorm;
183  fresidual = fresidual - c(:,m)*(NV(:,m))^t;
184  sumNsq = sumNsq + (NV(:,m).^2)^t;
185  usedcenteridx(m) = maxidx;
186  end
187  usedcenteridx = usedcenteridx(1:m);
188  nbase = NV(:,1:m);
189  ai = c(:,1:m);
190 
191  usedcenteridx = sort(usedcenteridx," ascend ");
192  nbase = nbase(usedcenteridx,:);
193  if length(usedcenteridx) < N && nargout < 3
194  error(" Less basis translates required than given, but their indices are not considered as output. Please receive the 'used' output and select the indicated subset as centers. ");
195  end
196  else
197  nbase = 1;
198  ai = (this.K\fxi" ) ";
199  usedcenteridx = 1:N;
200  end
201  }
215  function init(kernels.KernelExpansion kexp) {
216  this.K= [];
217  this.kexp= [];
218  if this.UseNewtonBasis
219  this.kexp= kexp;
220  else
221  this.K= kexp.getKernelMatrix;
222  end
223  }
235  function [rowvecci , integersvidx , sf ] = computeKernelCoefficients(rowvec<double> fxi,unused1) {
236 
237  [ci, base, svidx] = this.interpolate(fxi);
238  /* Here: Transform the coefficients back to direct translate
239  * basis as the Base cannot be set here and might be different
240  * for each component function */
241  if this.UseNewtonBasis
242  ci = ci / base;
243  end
244  sf = StopFlag.SUCCESS;
245  }
259  public: /* ( Static ) */
260 
261 
262  static function test_KernelInterpolation() {
263 
264  pm = PlotManager(false,2,2);
265  pm.LeaveOpen= true;
266 
267 /* x = -5:.05:5;
268  * fx = sinc(x); */
269 
270  x = 0:.05:10;
271  fx = x.^2.*sin(2*x);
272 
273  n = length(x);
274  samp = 1:15:n;
275 
276  ke = kernels.KernelExpansion;
277 
278  ke.Kernel= kernels.GaussKernel(.5);
279  internal_test(x,fx,false);
280  internal_test(x,fx,true);
281 
282  internal_test(x,ones(size(x))*5,false);
283  internal_test(x,ones(size(x))*5,true);
284 
285  ke.Kernel= kernels.InvMultiquadrics(-1.4,2);
286  internal_test(x,fx,true);
287  internal_test(x,fx,false);
288 
289  ke.Kernel= kernels.InvMultiquadrics(-4,5);
290  internal_test(x,fx,true);
291  internal_test(x,fx,false);
292 
293  /* 2D test */
294  fx = [fx; x.^3.*sin(.4*x)];
295  internal_test(x,fx,true);
296  internal_test(x,fx,false);
297 
298  pm.done;
299 
300  function internal_test(x,fx,lu)
301  xi = x(samp);
302  fxi = fx(:,samp);
303 
304  ki = general.interpolation.KernelInterpol;
305 
306  ke.Centers.xi= xi;
307  ki.UseNewtonBasis= lu;
308  ki.init(ke);
309 
310  [ke.Ma, ke.Base] = ki.interpolate(fxi);
311 
312  fsvr = ke.evaluate(x);
313 
314  h = pm.nextPlot(" xx ",sprintf(" Interpolation test with kernel %s, newton=%d ",class(ke.Kernel),lu));
315  hold(h," on ");
316  for k=1:size(fx,1)
317  plot(h,x,fx(k,:)," r ");
318  /* Plot approximated function */
319  plot(h,x,fsvr(k,:)," b-- ");
320  /* skipped = setdiff(1:length(x),svidx); */
321  plot(h,xi,fxi(k,:)," .r ");
322  end
323  end
324  }
331  static function test_KernelInterpolationError() {
332  realprec = 20;
333  range = [-4 4];
334 
335  pm = PlotManager(false,2,3);
336  pm.LeaveOpen= true;
337 
338  ke = kernels.KernelExpansion;
339  ke.Kernel= kernels.GaussKernel(1);
340  testfun = @(x)sinc(x) * 2 .*sin(3*x);
341  ke.Kernel= kernels.InvMultiquadrics(-1,1);
342 
343  ki = general.interpolation.KernelInterpol;
344 
345  hsteps = [.5:.25:2 2:2:10];
346  err = zeros(size(hsteps));
347  for n = 1:length(hsteps)
348  h = 1/hsteps(n);
349  xi = range(1):h:range(2);
350  xi = xi + rand(size(xi))*max(xi)/100+.1;
351  fxi = testfun(xi);
352 
353  x = range(1):h/realprec:range(2);
354  fx = testfun(x);
355 
356  ke.Centers.xi= xi;
357  ki.init(ke);
358  [ke.Ma, ke.Base, used] = ki.interpolate(fxi);
359  ke.Centers.xi= xi(:,used);
360  fint = ke.evaluate(x);
361 
362  ax = pm.nextPlot([" step " n],sprintf(" h=%g ",h)," x "," fx ");
363  plot(ax,xi,fxi," r. ",x,fx," r ",x,fint," b ");
364  err(n) = max(abs(fx-fint));
365  end
366  pm = PlotManager; pm.LeaveOpen= true;
367  ax = pm.nextPlot(" errors "," Error development over h sizes "," 1/h "," errors ");
368  h = 1./hsteps;
369  semilogy(ax,hsteps,exp(log(h)./h)," b ",hsteps,exp(-1./h)," b-- ",hsteps,err," r ");
370  legend(ax," exp(log(h)/h) "," exp(-1/h) "," ||f-sfx||_\infty ");
371  pm.done;
372  }
373 
374 
375 };
376 }
377 }
378 
379 
380 
Provides kernel interpolation.
A double value.
Base class for any KerMor class.
Definition: KerMorObject.m:17
static function test_KernelInterpolation()
Performs a test of this class.
sort
ort the handle objects in any array in ascending or descending order.
double RelTol
Maximum relative error tolerance (L2 in function dimension) over the given training data...
function registerProps(varargin)
Call this method at any class that defines DPCM observed properties.
Definition: DPCMObject.m:125
PlotManager: Small class that allows the same plots generated by some script to be either organized a...
Definition: PlotManager.m:17
logical MultiTargetComputation
A flag that indicates to users if the coefficient computation method is capable of using a matrix of ...
function [ matrix< double > ai , matrix< double > nbase , usedcenteridx ] = interpolate(fxi)
Computes the kernel expansion coefficients .
static const SUCCESS
Algorithm terminated otherwisely successful.
Definition: StopFlag.m:83
A boolean value.
logical LeaveOpen
Flag indicating if the plots should be left open once the PlotManager is deleted (as variable) ...
Definition: PlotManager.m:213
KerMorObject()
Constructs a new KerMor object.
Definition: KerMorObject.m:55
logical UseNewtonBasis
Flag that indicates whether to apply the Newton basis for stable interpolation . Using this interpol...
Interface for kernel expansion coefficient computation.
function [ rowvec ci , integer svidx , sf ] = computeKernelCoefficients(rowvec< double > fxi, unused1)
Implementation of the kernels.ICoeffComp interface.
static function test_KernelInterpolationError()
static function rowvec< double > n = L2(matrix< double > x)
Returns the discrete norm for each column vector in x.
Definition: Norm.m:39
FileMatrix: File-based matrix which stores sets of columns in separate files.
Definition: FileMatrix.m:18
Norm: Static class for commonly used norms on sets of vectors.
Definition: Norm.m:17
function init(kernels.KernelExpansion kexp)
% IKernelCoeffComp interface members Initializes the interpolation
KernelExpansion: Base class for state-space kernel expansions.
StopFlag: Flags that algorithms can use to specify the reason for their termination.
Definition: StopFlag.m:17