39 xP(cnt) = area(1) + rand * (area(2)-area(1));
40 yP(cnt) = area(1) + rand * (area(2)-area(1));
45 yP = [-2.5 .2 1 .5 5];
59 k = kernels.GaussKernel(gamma);
62 range = area(1):dt:area(2);
63 [
X,
Y] = meshgrid(range,range);
66 vals = zeros(size(
X,1),size(
X,2),kNum);
68 arg = sqrt((
X-xP(cnt)).^2+(
Y-yP(cnt)).^2);
69 vals(:,:,cnt) = c(cnt)*k.evaluateScalar(arg);
74 D = getDistanceMat(xP,yP);
77 nullrad = sqrt(-gamma*log(epsi./abs(c)));
78 nullrad = repmat(nullrad,kNum,1);
79 F = k.evaluateScalar(D);
95 lipFun = @k.getImprovedLocalSecantLipschitz;
136 cm = datacursormode(h);
138 cm.SnapToDataVertex=
" on ";
139 cm.DisplayStyle=
" window ";
140 cm.Updatefcn= @pointSelected;
154 h = contour3(
X,
Y,Z,20,
" black ");
156 lineobj =
cell.empty(1,0);
161 function updatePlot(xp,yp)
165 [estMax, di, lip] = getEstGradient(xp,yp);
166 [effMax, xm, ym, dist, Zsecant] = getEffGradient(xp,yp);
170 xidiff = [xP - xp; yP-yp];
171 Umat = xidiff^
t*xidiff;
174 sameside = Umat(idx,:) > 0;
175 [estMaxLoc, didummy, lipdummy] = getEstGradientJan(xp,yp,sameside);
176 em(end+1) = abs(estMaxLoc);
185 [dummy, yi] = min(abs(range - xp));
186 [dummy, xi] = min(abs(range - yp));
191 for idx=1:length(lineobj)
193 if ~isempty(lin) && ishandle(lin)
213 fxi = c*k.evaluate(centers,centers);
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);
219 lineobj[end+1] = plot3(xP(idx),yP(idx),fxi(idx),
" black. ",
" MarkerSize ",15);
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);
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);
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;
241 [maxp, idp] = max(xisec);
275 experi = sum( (posi .*
F(idp,:) + abs(neg) .*
F(idm,:)));
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);
283 experi2 = sum( (posi .* W(idp,:) + abs(neg) .* W(idm,:)));
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);
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)));
308 h = contour3(
X,
Y,Zs,20,
" black ");
309 [dummy, maxi] = max(Zs(:));
310 plot3(
X(maxi),
Y(maxi),Zs(maxi),
" black* ",
" MarkerSize ",15);
317 function txt = pointSelected(hObject, eventdata)
318 info = getCursorInfo(cm);
320 if ~isequal(oldpos,pos(1:2))
322 updatePlot(pos(1),pos(2));
327 function [estMax, di, lip] = getEstGradient(xp,yp)
329 di = sqrt((xP-xp).^2 + (yP-yp).^2);
330 lip = lipFun(di,Inf,0,[]);
332 estMax = abs(c) * lip^
t;
335 function [estMax, di, lip] = getEstGradientJan(xp,yp,sameidx)
337 di = sqrt((xP-xp).^2 + (yP-yp).^2);
338 liptest = lipFun(di,Inf,0,[]);
340 lip = lipFun(di,Inf,0,[]);
342 estMax = abs(c(sameidx)) * lip^
t;
345 function [effMax, xm, ym, dist, sgrad] = getEffGradient(xp,yp)
347 [dummy, yi] = min(abs(range - xp));
348 [dummy, xi] = min(abs(range - yp));
353 fdiff = Z - Z(xi,yi);
354 xdiff = sqrt((
X-xp).^2+(
Y-yp).^2);
355 sgrad = fdiff ./ (xdiff+eps);
357 [val, id] = max(abs(sgrad(:)));
358 [xm,ym] = ind2sub(size(sgrad),
id);
361 dist = sqrt((
X(xi,yi)-
X(xm,ym)).^2 + (
Y(xi,yi)-
Y(xm,ym)).^2);
363 effMax = (Z(xm,ym)-Z(xi,yi)) / dist;
366 function D = getDistanceMat(xP,yP)
372 D = sqrt((ones(n,1)*nsq)
" + ones(n,1)*nsq - 2*(vec "*vec));
375 function A = getAngleMat(xP,yP)
378 no = sqrt(sum(vec.^2));
383 function A = getAngle(v,xP,yP)
386 no1 = sqrt(sum(vec.^2));
387 no2 = sqrt(sum(v.^2));
A MatLab cell array or matrix.
function LocalLipGradientEstPlay()