-
Notifications
You must be signed in to change notification settings - Fork 0
/
ism_steepDesc.m
99 lines (78 loc) · 2.24 KB
/
ism_steepDesc.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
function [x,f, exitflag,costdata] = ism_steepDesc(f,x0,tol,maxit)
%
%
% This code comes with no guarantee or warranty of any kind.
%
% function [x,histout,costdata] = steep(x0,f,tol,maxit)
%
% steepest descent, simple linesearch
%
%
% Input: x0 = initial iterate
% f = objective function,
% the calling sequence for f should be
% [fout,gout]=f(x) where fout=f(x) is a scalar
% and gout = grad f(x) is a COLUMN vector
% tol = termination criterion norm(grad) < tol
% optional, default = 1.d-6
% maxit = maximum iterations (optional) default = 1000
%
% Output: x = solution
% histout = iteration history
% Each row of histout is
% [norm(grad), f, number of step length reductions, iteration count]
% costdata = [num f, num grad, num hess] (for steep, num hess=0)
%
%
% linesearch parms
if nargin < 4 %Maximum number of iterations
maxit=35;
end
if nargin < 3 %Tolerance, as fraction (xc - xn)/xc
tol=0.05;
end
itc=1; xc=x0; %Initialize
[fc,gc]=feval(f,xc); %cost,gradient
gc = gc/max(abs(gc)); %Normalize
costdata = zeros(maxit,1);
costdata(1) = fc;
chng = Inf;
exitflag = 0;
tau = 0.7; %Line stepping parameters
disp(['Initial Cost: ', num2str(fc)])
while(chng > tol && itc <= maxit)
disp(['Iteration: ', num2str(itc)])
lambda = 1;
xn=xc-lambda*gc;
fn = feval(f,xn);
iarm = 1;
fp = Inf;
while(((fn > fc) && (fp > fc)) || fn < fp) %line search
fp = fn;
xp = xn;
lambda = lambda*tau;
xn=xc-lambda*gc;
fn=feval(f,xn);
if (iarm > 15)
disp('Error in steepest descent ')
x = xc;
f = fn;
exitflag = 1;
return; end
iarm=iarm+1;
end
chng = (fc-fp)/fc; %Iteration information
itc = itc + 1;
disp(['Updated Cost: ', num2str(fp)])
disp(['% Change: ', num2str(100*chng)])
xc=xp;
fc = fp;
if chng > tol, %Update Values
[fc,gc]=feval(f,xc);
gc = gc/max(abs(gc));
end;
costdata(itc) = fc;
end
x=xc;
f= fc;
end