-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoglegfinal.m
58 lines (48 loc) · 904 Bytes
/
doglegfinal.m
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function [pk] = doglegfinal(xk,dk)
%dogleg podla matonohy str 46
gk=ngrad(xk);
Hk=nhess(xk);
%1) kontrola poz def
if (gk'*Hk*gk)<0
pk=cauchy(xk,dk); %ak hessian nieje poz def pouzijem cauchyho bod
%disp('1');
return
end
% spocitam cacuhyho bod
xc=((-gk'*gk)/(gk' * Hk * gk))*gk;
%2) cauchyho bod lezi mimo trust regionu
if norm(xc)>=dk
pk=(-dk/norm(gk))*gk;
%disp('2');
return
end
% zrataj newtnov bod
xn=Hk\(-gk);
%3) kontrola klesania
if xc'*(xn-xc)<=0
pk=cauchy(xk,dk);
%disp('3');
return
end
%4) Newtnov bod lezi v trust regione
if norm(xn)<=dk
pk=xn;
%disp('4');
return
end
%5) riesenie na doglegu
k1=sum((xn-xc).^2);
k2=sum(2*xc.*(xn-xc));
k3=sum(xc.^2)-dk.^2;
t=roots([k1 k2 k3]) ;
if ((t(1)<1) && (t(1)>=0))
pk=xc+t(1)*(xn-xc);
else
if ((t(2)<1) && (t(2)>=0))
pk=xc+t(2)*(xn-xc);
else
pk=NaN;
end
end
%disp('5');
end