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
ResponseSurfaceApprox.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 
19  :public handle {
29 public:
30 
31  EC;
32 
33  XYRange = -5:.1:5;
34 
35  PM;
36 
37  TrainPerc = .2;
38 
39  NumSteps = 10;
40 
42 
43 
44 public:
45 
46  X;
47 
48  Y;
49 
50  Z;
51 
52  atd;
53 
54 
55 public:
56 
58  ec = kernels.config.ExpansionConfig;
59  ec.Prototype.Kernel= kernels.Wendland;
60  ec.StateConfig= kernels.config.WendlandConfig(" G ",10," S ",2," Dim ",2);
61  this.EC= ec;
62  pm = PlotManager(false,3,2);
63  pm.LeaveOpen= true;
64  this.PM= pm;
65  }
66 
67 
68  function Z = getSurface(X,Y) {
69  Z = sin(X/2).*Y.^2+5*X+Y;
70  this.Z= Z;
71  }
72 
73 
74  function alg = setupVKOGA() {
75  alg = approx.algorithms.VKOGA;
76  alg.MaxExpansionSize= this.NumSteps;
77  alg.UsefPGreedy= 0;
78  alg.ExpConfig= this.EC;
79  }
80 
81 
82  function kernels.KernelExpansionkexp = compute() {
83  this.init;
84  alg = this.setupVKOGA;
85  kexp = alg.computeApproximation(this.atd);
86  }
87 
88 
89  function init(sel) {
90  [X,Y] = meshgrid(this.XYRange);
91  if nargin < 2
92  tot = numel(X);
93  sel = randperm(tot);
94  n = round(this.TrainPerc*tot);
95  end
96  Z = this.getSurface(X,Y);
97  this.Z= Z;
98  fxi = Z(sel(1:n));
99  xi = [X(sel(1:n)); Y(sel(1:n))];
100  atd = data.ApproxTrainData(xi,[],[]);
101  atd.fxi= fxi;
102  this.X= X;
103  this.Y= Y;
104  this.atd= atd;
105  }
106 
107 
108  function plotOriginal() {
109  h = this.PM.nextPlot(" fun "," Original response surface "," x_1 "," x_2 ");
110  surfl(h, this.X, this.Y, this.Z);
111  zlabel(" f(x) ");
112  shading interp;
113  }
114 
115 
116  function plotOriginalWithTD() {
117  h = this.PM.nextPlot(" fun_atd "," Original response surface with training data "," x "," f(x) ");
118  surf(h, this.X, this.Y, this.Z);
119  shading interp;
120  hold(h," on ");
121  xi = this.atd.xi.toMemoryMatrix;
122  plot3(h,xi(1,:),xi(2,:),fxi," r. ");
123  }
124 
125 
126  function plotTrainData() {
127  h = this.PM.nextPlot(" traindata "," Training data "," x_1 "," x_2 ");
128  xi = this.atd.xi.toMemoryMatrix;
129  plot3(h,xi(1,:),xi(2,:),fxi," r. ");
130  }
131 
132 
134  if nargin < 3
135  pm = this.PM;
136  end
137  allxi = [this.X(:) this.Y(:)]^t;
138 
139  /* aZ = kexp.evaluate(allxi);
140  * aZ = reshape(aZ,size(X,1),[]);
141  * h = pm.nextPlot('approx','Approx','x','f(x)');
142  * surf(h, X, Y, aZ);
143  * h = pm.nextPlot('error','Error','x','f(x)');
144  * surf(h, X, Y, abs(Z-aZ)); */
145  args = [" EdgeColor "," interp "," FaceAlpha ",.2," EdgeColor "," none "," FaceColor "," red "];
146 
147  centerpos = Utils.findVecInMatrix(allxi,kexp.Centers.xi);
148 
149  /* % Iteration plots */
150  zlim = [];
151  for s = 0:this.NumSteps
152  if s == 0
153  fxi = zeros(size(this.X));
154  else
155  k = kexp.getSubExpansion(s);
156  fxiflat = k.evaluate(allxi);
157  fxi = reshape(fxiflat,size(this.X,1),[]);
158  end
159  if isa(pm," PlotManager ")
160  h = pm.nextPlot(sprintf(" iter%d ",s),...
161  sprintf(" Approximation at step %d ",s)," x_1 "," x_2 ");
162  else
163  h = pm;
164  cla(h);
165  end
166  surf(h, this.X, this.Y, fxi," EdgeColor "," interp ");
167  zlabel(" f(x) ");
168  hold(h," on ");
169  surf(h, this.X, this.Y, this.Z,args[:]);
170  if s > 0
171  plot3(allxi(1,centerpos(s)),allxi(2,centerpos(s)),...
172  fxiflat(1,centerpos(s))," y* "," MarkerSize ",20);
173  plot3(allxi(1,centerpos(s)),allxi(2,centerpos(s)),...
174  fxiflat(1,centerpos(s))," k. "," MarkerSize ",20);
175  if s > 1
176  plot3(allxi(1,centerpos(1:s-1)),allxi(2,centerpos(1:s-1)),...
177  fxiflat(1,centerpos(1:s-1))," r. "," MarkerSize ",16);
178  end
179  end
180  /* h = pm.nextPlot(sprintf('err_iter%d',s),...
181  * sprintf('Error at step %d',s),'x','f(x)');
182  * surf(h, X, Y, abs(Z-fxi),'EdgeColor','interp');
183  *axis(h,'tight'); */
184  if ~isempty(this.LoopCallback)
185  this.LoopCallback(this,s,h);
186  end
187  if isempty(zlim)
188  zlim = get(h," ZLim ");
189  else
190  set(h," ZLim ",zlim);
191  end
192  end
193 
194  if nargin < 2
195  pm.done;
196  end
197  }
198 
199 
200 
201 public: /* ( Static ) */
202 
203  static function run() {
204 
205  rsa = demos.ResponseSurfaceApprox;
206  kexp = rsa.compute;
207  rsa.iterationPlots(kexp);
208  }
216 };
217 }
218 
Collection of generally useful functions.
Definition: Utils.m:17
static function run()
This demo shows how the VKOGA algorithm approximates a 2D-1D response surface for different kernel co...
static function idx = findVecInMatrix(A, b)
Finds column vectors inside a matrix.
Definition: Utils.m:259
function subexp = getSubExpansion(arg)
reshape
hanges the dimensions of the handle object array to the specified dimensions. See the MATLAB reshape ...
This demo shows how the VKOGA algorithm approximates a 2D-1D response surface for different kernel co...
PlotManager: Small class that allows the same plots generated by some script to be either organized a...
Definition: PlotManager.m:17
Matlab's base handle class (documentation generation substitute)
function kernels.KernelExpansion kexp = compute()
function iterationPlots(kernels.KernelExpansion kexp, pm)
struct Centers
The kernel centers used in the approximation.
KernelExpansion: Base class for state-space kernel expansions.