-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest_linesearch.m
56 lines (43 loc) · 1.16 KB
/
test_linesearch.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
%% GSS
figure; hold on;
a=0; % start of interval
b=2; % end of interval
epsilon=0.000001; % accuracy value
iter= 50; % maximum number of iterations
tau=double((sqrt(5)-1)/2); % golden proportion coefficient, around 0.618
k=0; % number of iterations
x1=a+(1-tau)*(b-a); % computing x values
x2=a+tau*(b-a);
f_x1=f(x1); % computing values in x points
f_x2=f(x2);
plot(x1,f_x1,'rx') % plotting x
plot(x2,f_x2,'rx')
while ((abs(b-a)>epsilon) && (k<iter))
k=k+1;
if(f_x1<f_x2)
b=x2;
x2=x1;
x1=a+(1-tau)*(b-a);
f_x1=f(x1);
f_x2=f(x2);
plot(x1,f_x1,'rx');
else
a=x1;
x1=x2;
x2=a+tau*(b-a);
f_x1=f(x1);
f_x2=f(x2);
plot(x2,f_x2,'rx')
end
k=k+1;
end
% chooses minimum point
if(f_x1<f_x2)
sprintf('x_min=%f', x1)
sprintf('f(x_min)=%f ', f_x1)
plot(x1,f_x1,'ro')
else
sprintf('x_min=%f', x2)
sprintf('f(x_min)=%f ', f_x2)
plot(x2,f_x2,'ro')
end