Skip to content

Commit e2f6472

Browse files
final commit
1 parent 835fa40 commit e2f6472

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

goldenSection.m

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
clc
2+
clear
3+
close all
4+
%% declaring ackely function
5+
ackley = @(X,Y)-20 * exp(-0.2 * sqrt(0.5*(X.^2 + Y.^2))) - exp(0.5*(cos(2*pi*X) + cos(2*pi*Y))) + exp(1) + 20;
6+
7+
%% golden section implementation
8+
% defining and declaring variables
9+
a = -5.0;
10+
b = 5.0;
11+
x = a:0.1:b;
12+
gammaInv = 0.618;
13+
L0 = b - a;
14+
L2_star = gammaInv.^2 * L0;
15+
x1 = a + L2_star;
16+
x2 = b - L2_star;
17+
n = 10;
18+
19+
% ploting the function value with initial points
20+
figure(1)
21+
plot(x,ackley(x,0));
22+
title('golden section method on ackley function initial situation');
23+
xlabel('x');
24+
ylabel('f');
25+
hold on
26+
plot(a,ackley(a,0),'*k');
27+
plot(x1,ackley(x1,0),'*m');
28+
plot(x2,ackley(x2,0),'*r');
29+
plot(b,ackley(b,0),'*g');
30+
legend('f','a','x1','x2','b');
31+
hold off
32+
33+
figure(2)
34+
plot(x,ackley(x,0));
35+
title('golden section method on ackley function final situation');
36+
xlabel('x');
37+
ylabel('f');
38+
hold on
39+
40+
for j = 1:n
41+
f1 = ackley(x1,0);
42+
f2 = ackley(x2,0);
43+
% checking unimodality condition and
44+
% assigning new a,x1,x2 and b accordingly
45+
if f2 > f1
46+
x3 = a + (x2 - x1);
47+
b = x2;
48+
x2 = x1;
49+
x1 = x3;
50+
elseif f1 > f2
51+
x3 = x1 + (b - x2);
52+
a = x1;
53+
x1 = x2;
54+
x2 = x3;
55+
else
56+
a = x1;
57+
b = x2;
58+
L0 = b - a;
59+
L2_star = 0.382 * L0;
60+
x1 = a + L2_star;
61+
x2 = b - L2_star;
62+
end
63+
end
64+
% plot the final point along with their function value
65+
plot(a,ackley(a,0),'*k');
66+
plot(x1,ackley(x1,0),'*m');
67+
plot(x2,ackley(x2,0),'*r');
68+
plot(b,ackley(b,0),'*g');
69+
legend('f','a','x1','x2','b');
70+
hold off
71+
% display the final interval
72+
Ln = [a b];
73+
display(Ln);

interiorPenalty.m

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
clc
2+
clear
3+
close all
4+
%% defining three hump camel function
5+
three_hump_camel = @(X,Y) 2*X.^2 - 1.05*X.^4 + X.^6/6 + X.*Y + Y.^2;
6+
%% surface ploting three hump camel function
7+
[X,Y] = meshgrid(-5:.1:5, -5:.1:5);
8+
Z = three_hump_camel(X,Y);
9+
% display the surface plot of the function
10+
figure(1)
11+
surf(X,Y,Z)
12+
title('surface plot of three hump camel functoin');
13+
xlabel('x(1)');
14+
ylabel('x(2)');
15+
zlabel('threeHumpCamel');
16+
shading interp
17+
%% algorithm implementation
18+
phi_ = @(x,r) 2*x(1)^2 - 1.05*x(1)^4 + (x(1)^6)/6 + x(1)*x(2) + x(2)^2 - r * (1/(-x(1)-5) + 1/(-x(2)-5) + 1/(x(1)-5) + 1/(x(2)-5));
19+
% defining variables
20+
r = 1000.0;
21+
c = 0.01;
22+
epsilon = 0.005;
23+
options = optimoptions(@fminunc,'Algorithm','quasi-newton');
24+
% display contour plot and converging points
25+
figure(2)
26+
contour(X,Y,Z);
27+
title('contour of f(x,y) converges at black point');
28+
xlabel('x(1)');
29+
ylabel('x(2)');
30+
shading interp
31+
hold on
32+
% declaring initial point x1
33+
x1 = [-4.5, 2.5];
34+
f1 = three_hump_camel(x1(1), x1(2));
35+
scatter(x1(1),x1(2),'*');
36+
iteration = 0;
37+
% while function not converge keep iterating
38+
while true
39+
iteration = iteration + 1;
40+
phi = @(x)phi_(x,r);
41+
[x2,opt_phi] = fminunc(phi, x1, options);
42+
f2 = three_hump_camel(x2(1), x2(2));
43+
dx = x2 - x1;
44+
quiver(x1(1),x1(2),dx(1),dx(2),0);
45+
% convergence check
46+
if abs((f2 - f1)/f2) <= epsilon
47+
min_f = f2;
48+
x_star = x2;
49+
display('function Converged!');
50+
fprintf('Number of Iteration taken to converge = %i\n', iteration);
51+
fprintf('Minimum value of function = %f\n', min_f);
52+
display('Optimal point is');
53+
display(x_star);
54+
scatter(x_star(1),x_star(2),'k*');
55+
break
56+
end
57+
scatter(x2(1),x2(2),'*');
58+
r = c * r;
59+
x1 = x2;
60+
f1 = f2;
61+
end
62+
legend('contour','intermediate point','direction of minima');
63+
hold off

0 commit comments

Comments
 (0)