-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsteepestDescent.m
49 lines (31 loc) · 965 Bytes
/
steepestDescent.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
function [Y,X,iter] = steepestDescent(f,gradient,N_max,x0,e1,e2,e3,lowerLimit,upperLimit)
% Optimization Theory
% Steepest Descent Algorithm
% 28/11/2020
iter = 0;
xk = x0;
while 1
pk = -1.*gradient(xk(1),xk(2)) ;
g = @(sk) ( f(xk(1)+sk*pk(1),xk(2)+sk*pk(2)));
[x2_g] = goldenSection(g,lowerLimit,upperLimit,1e-9);
sk = x2_g;
old_xk = xk;
xk = xk + sk.*pk;
iter = iter +1;
if iter>N_max
fprintf('k becomes larger then Nmax\n');
break;
elseif abs(( f(xk(1),xk(2)) - f(old_xk(1),old_xk(2)) )) < e1
fprintf('Fx+1 - Fx<e1');
break;
elseif norm(xk - old_xk)<e2
fprintf('norm of x+1 - x<e2');
break;
elseif norm(gradient(xk(1),xk(2)))<e3
fprintf('norm of Fx+1<e2');
break;
end
end
Y = f(xk(1),xk(2));
X = xk;
end