-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstep.m
55 lines (48 loc) · 853 Bytes
/
step.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
function tk = step( f , x0 )
tg = 0 ;
td = 0 ;
t = 1/2 ;
tk = 0 ;
while tk ~= t
[a,b,c] = golstein_rule( f, x0, t) ;
if a == 1
tk = t ;
end
if b == 1
td = t ;
end
if c == 1
tg = t ;
end
if b == 1 || c == 1
if td == 0
t = 10*tg ;
else
t = (tg + td)/2 ;
end
end
end
end
function [ val , grad0 ] = h( f, x0, t )
d = -eval(subs(jacobian(f),argnames(f),x0)) ;
x_delta = x0 + t*d ;
val = eval(subs(f,argnames(f),x_delta)) - eval(subs(f,argnames(f),x0)) ;
grad0 = eval(subs(gradient(f),argnames(f),x0))*d ;
end
function [ a , b , c ] = golstein_rule( f, x0, t )
a = 0 ;
b = 0 ;
c = 0 ;
m2 = 0.618033989 ;
m1 = 1 - m2 ;
[h_valt, grad_h0] = h(f, x0, t) ;
if ( h_valt > m1*grad_h0*t )
b = 1 ;
end
if ( h_valt < m2*grad_h0*t )
c = 1 ;
end
if b == 0 && c == 0
a = 1 ;
end
end