-
Notifications
You must be signed in to change notification settings - Fork 0
/
dichotomous_search.m
47 lines (36 loc) · 1.14 KB
/
dichotomous_search.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
% Function to Maximize (Only Unimodal Function)
f = @(x) 4*sin(x)*(1 + cos(x));
% Plot
fplot(f,[-1,7],'b', LineWidth=2);
hold on; grid on;
title('Plot of f(x)');
xlabel('x'); ylabel('f(x)');
% Initiation
a = 0;
b = pi/2;
epsilon = 0.2;
stop = 0.5;
% Dichotomous search algorithm
dichotomous_search(f, a, b, epsilon,stop);
function dichotomous_search(f, a, b, epsilon, stop)
i = 1; %number of iterations
while (abs(a - b) > stop)
x1 = (a + b - epsilon) / 2;
x2 = (a + b + epsilon) / 2;
if f(x1) > f(x2)
b = x2;
else
a = x1;
end
disp([num2str(i) ': ' num2str(x1) ' , ' num2str(x2)]); % x1, x2 Values
disp([' {' num2str(f(x1)) ' , ' num2str(f(x2)) '}']); % x1 x2 function values
disp([' [', num2str(a) ' , ' num2str(b) ']']); % New intervals
scatter([a, b], [f(a), f(b)], LineWidth=2); %Plot new interval
i = i + 1; %Increment Interation
end
% Maximum value
x_max = (a + b) / 2;
% Display result
disp(['Minimum: (', num2str(x_max) ',' num2str(f(x_max)) ')']);
stem(x_max, f(x_max), LineWidth=2);hold off;
end