-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdexpr.m
More file actions
41 lines (33 loc) · 1005 Bytes
/
dexpr.m
File metadata and controls
41 lines (33 loc) · 1005 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function d=dexpr(p,fin,nit,alpha)
% Copyright (C) 2004-2012 Per-Olof Persson. See COPYRIGHT.TXT for details.
if nargin<3, nit=20; end
if nargin<4, alpha=0.1; end
fx=inline(vectorize(maple(['diff(',fin,',x)'])),'x','y');
fy=inline(vectorize(maple(['diff(',fin,',y)'])),'x','y');
fxx=inline(vectorize(maple(['diff(',fin,',x$2)'])),'x','y');
fyy=inline(vectorize(maple(['diff(',fin,',y$2)'])),'x','y');
fxy=inline(vectorize(maple(['diff(diff(',fin,',x),y)'])),'x','y');
f=inline(vectorize(fin),'x','y');
x0=p(:,1);
y0=p(:,2);
x=x0;
y=y0;
for it=1:nit
cf=feval(f,x,y);
cfx=feval(fx,x,y);
cfy=feval(fy,x,y);
cfxx=feval(fxx,x,y);
cfxy=feval(fxy,x,y);
cfyy=feval(fyy,x,y);
F1=cf;
F2=(x-x0).*cfy-(y-y0).*cfx;
J11=cfx;
J12=cfy;
J21=cfy+(x-x0).*cfxy-(y-y0).*cfxx;
J22=-cfx-(y-y0).*cfxy+(x-x0).*cfyy;
detJ=J11.*J22-J12.*J21;
detJ(detJ==0)=inf;
x=x-alpha*(J22.*F1-J21.*F2)./detJ;
y=y-alpha*(-J12.*F1+J11.*F2)./detJ;
end
d=sqrt((x-x0).^2+(y-y0).^2).*sign(feval(f,x0,y0));