-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsteepest_backtrack.m
More file actions
62 lines (51 loc) · 1.4 KB
/
Copy pathsteepest_backtrack.m
File metadata and controls
62 lines (51 loc) · 1.4 KB
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
59
60
61
62
function F = steepest_backtrack(x1,ro,c,itr_num)
f = @(x) 100*(x(2)-x(1)^2)^2+(1-x(1))^2 ;
gf = @ (x) [-2*(1-x(1))-400*x(1)*(x(2)-x(1)^2) ; 200*(x(2)-x(1)^2)] ;
%x0 = [0,1]'
alpha = .9*ones(1,itr_num) ;
X = ones(2,itr_num) ;
F = ones(1,itr_num) ;
%P=[0;0]
x_nxt = x1 ;
for k = 1:itr_num
X(:,k) = x_nxt ;
P = -gf( X(:,k) ) ;
while ( f( X(:,k) + alpha(k) * P) > f( X(:,k)) + c*alpha(k)* gf( X(:,k))' * P )
alpha(k) = ro * alpha (k) ;
end
F(k) = f(X(:,k)) ;
x_nxt = X(:,k) + alpha(k) * P ;
end
% cvx_begin
% variable x(2)
% minimize(100*(x(2)-x(1)^2)^2+(1-x(1))^2 )
% subject to
%
% cvx_end
% x
disp('X')
X(:,end)
disp('alpha')
alpha(end)
%scatter(x,y,sz,c,'filled')
figure
subplot(1,2,1);
plot(1:itr_num , alpha)
%indexmax = find(max(F) == F);
%Fmax = F(indexmax);
%strmin = ['F* = ',num2str(F(end))];
%text(itr_num/2,Fmax,strmin,'HorizontalAlignment','left');
%
%strmin = ['X* = ',num2str(F(:,end))];
%text(itr_num/2,Fmax/2,strmin,'HorizontalAlignment','left');
title(strcat(' x_0 = ',mat2str(x1)))
xlabel('iteration')
ylabel('alpha(BackTraking)')
subplot(1,2,2);
plot(1:itr_num , log(F))
%surf(X(1,:) , X(2,:) , F)
title(strcat('Steepest Decent ', ' x_0 = ',mat2str(x1) , ' F* = ' ,mat2str(F(end)) , ' X* = ' ,mat2str(X(:,end)) ))
xlabel('iteration')
ylabel('log(F_k*)')
return
end