-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathExhaustiveSearchMethod.m
61 lines (49 loc) · 1.36 KB
/
ExhaustiveSearchMethod.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
% Exhaustive Search Method as Explained in the book
%"Optimization of Engineering Design: Algorithms and Examples" by Prof. Kalyanmoy Deb
% Code developed by Sri. Soumyabrata Bhattacharjee
% Matlab version R2022b
% Date: 14th October, 2018
% Date: 25th October, 2023 (Updated)
% Date: 27th October, 2023 (Updated)
clear all
clc
a=0.1; %Lower bound
b=14; % Upper bound
n = 1000; %Number of divisions
dx=(b-a)/n; %Length of each division
% Equation to be minimized
f=@(x) x.^2+54./x;
% Initialization
x1=a;
x2=x1+dx;
x3=x2+dx;
x_min=x2;
f_min=f(x_min);
while x3<=b
if f(x2)<=f(x1) && f(x2)<=f(x3)
disp(['The approx minimum point is: ' num2str(x2)])
disp(['The approx minimum function value is: ' num2str(f(x2))])
x_min=x2;
f_min=f(x_min);
end
x1=x2;
x2=x1+dx;
x3=x2+dx;
end
% Plotting
x = linspace(a,b,n);
plot(x,f(x),'LineWidth',2,'DisplayName',func2str(f));
xlabel('x',FontWeight='bold');
ylabel('f(x)','FontWeight','bold')
grid on;
title('Exhaustive Search Method','FontWeight','bold');
hold on;
% Scatter plot for the minimum point
scatter(x_min, f_min, 100, 'red', 'filled', 'DisplayName', 'Minimum Point');
% Adding Legend
legend('Location','best')
legend('show')
% Save the figure as a PNG file
saveas(gcf, 'Exhaustive Search Method.png');
% Display the plot
hold off;