Skip to content

Commit 6131f97

Browse files
authored
Add files via upload
1 parent 1af6c26 commit 6131f97

11 files changed

+938
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
function [x_k] = Newton_Opt(max_iter,min_tol)
2+
3+
4+
clear all;
5+
close all;
6+
7+
% Initialization
8+
max_iter=50; % maximum number of iterations
9+
min_tol=10^-6; % tolerance
10+
x_k1= zeros(2,1);
11+
p_k= zeros(2,1);
12+
alpha=1;
13+
14+
% Initial guess
15+
x_k=[-1.2;1];
16+
17+
k=0; tol=10;
18+
19+
xvec1(k+1)=x_k(1,1);
20+
xvec2(k+1)=x_k(2,1);
21+
22+
x=x_k(1,1); y=x_k(2,1);
23+
f_k=F(x,y);
24+
fvec(k+1)=f_k;
25+
26+
% Newton's algorithm
27+
while (k<=max_iter) && (tol>=min_tol)
28+
x=x_k(1,1); y=x_k(2,1);
29+
p_k=-1*(HF(x,y)\GradF(x,y));
30+
x_k1=x_k+(alpha*p_k);
31+
f_k=F(x,y);
32+
df_k=norm(GradF(x,y),Inf);
33+
x=x_k1(1,1); y=x_k1(2,1);
34+
f_k1=F(x,y);
35+
df_k1=norm(GradF(x,y),Inf);
36+
tol=abs(df_k1);
37+
fprintf('%3.0f\t %7.4f\t %7.4f\t %7.4f\t %7.4f\n',k, x_k,f_k,df_k);
38+
x_k=x_k1;
39+
k=k+1;
40+
xvec1(k+1)=x_k(1,1);
41+
xvec2(k+1)=x_k(2,1);
42+
fvec(k+1)=f_k1;
43+
end
44+
45+
figure;
46+
plot(xvec1,xvec2,'bo-','LineWidth', 1.5)
47+
set(gca, 'fontsize', 14, 'fontname', 'times');
48+
xlabel('x_1')
49+
ylabel('x_2')
50+
grid;
51+
52+
figure;
53+
plot3(xvec1,xvec2,fvec,'ro-','LineWidth', 2)
54+
set(gca, 'fontsize', 14, 'fontname', 'times');
55+
xlabel('x_1')
56+
ylabel('x_2')
57+
zlabel('f')
58+
grid;
59+
60+
%The declaration of the objective function
61+
function f= F(x,y)
62+
63+
f= 100*(y - x.^2)^2 + (1 - x)^2;
64+
65+
%The declaration of the Grad
66+
function g = GradF(x,y)
67+
68+
g= [400*x^3 - 400*x*y + 2*x - 2;
69+
200*(y - x^2)];
70+
71+
%The declaration of the Hessian Matrix
72+
function h = HF(x,y)
73+
74+
h= [1200*x^2 - 400*y + 2, -400*x;
75+
-400*x, 200];
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
function [x_k] = Newton_Opt(max_iter,min_tol)
2+
3+
clear all;
4+
close all;
5+
6+
% Initialization
7+
max_iter=100; % maximum number of iterations
8+
min_tol=10^-6; % tolerance
9+
x_k1= zeros(4,1);
10+
p_k= zeros(4,1);
11+
alpha=1;
12+
13+
% Initial guess
14+
%x_k=[1.1;1.2;1.3;1.4]; %easy start for Wood's f
15+
%x_k=[-3;-1;-3;-1]; %hard start for Wood's f
16+
x_k=[-3; -1; 0; 1]; %for Powell's f
17+
k=0; tol=10;
18+
19+
xvec1(k+1)=x_k(1,1);
20+
xvec2(k+1)=x_k(2,1);
21+
xvec3(k+1)=x_k(3,1);
22+
xvec4(k+1)=x_k(4,1);
23+
24+
x=x_k(1,1); y=x_k(2,1); w=x_k(3,1); z=x_k(4,1);
25+
f_k=F(x,y,w,z);
26+
fvec(k+1)=f_k;
27+
28+
% Newton's algorithm
29+
while (k<=max_iter) && (tol>=min_tol)
30+
%x=x_k(1,1); y=x_k(2,1); w=x_k(3,1); z=x_k(4,1);
31+
32+
p_k=-1*(HF(x,y,w,z)\GF(x,y,w,z));
33+
34+
x_k1=x_k+(alpha*p_k);
35+
f_k=F(x,y,w,z);
36+
df_k=norm(GF(x,y,w,z), Inf);
37+
x=x_k1(1,1); y=x_k1(2,1); w=x_k1(3,1); z=x_k1(4,1);
38+
f_k1=F(x,y,w,z);
39+
df_k1=norm(GF(x,y,w,z),Inf);
40+
tol=abs(df_k1);
41+
42+
fprintf('%3.0f\t %7.4f\t %7.4f\t %7.4f\t %7.4f\n',k, x_k,f_k,df_k);
43+
x_k=x_k1;
44+
k=k+1;
45+
xvec1(k+1)=x_k(1,1);
46+
xvec2(k+1)=x_k(2,1);
47+
xvec3(k+1)=x_k(3,1);
48+
xvec4(k+1)=x_k(4,1);
49+
fvec(k+1)=f_k1;
50+
end
51+
52+
function f= F(x,y,w,z)
53+
%Wood's function
54+
%f= 100*(y - x.^2)^2 + (1 - x)^2 + 90*(z - w.^2)^2 + (1 - w)^2 + 10.1*((y - 1)^2 + (z - 1)^2) + 19.8*(y-1)*(z-1);
55+
56+
%Powell's function
57+
f= (x + 10*y)^2 + 5*(w - z)^2 + (y - 2*w)^4 + 10*(x - z)^4;
58+
59+
function g = GF(x,y,w,z)
60+
%Wood's GradF
61+
%g=[2*x - 400*x*(- x^2 + y) - 2
62+
% - 200*x^2 + (1101*y)/5 + (99*z)/5 - 40
63+
% 2*w - 360*w*(- w^2 + z) - 2
64+
% - 180*w^2 + (99*y)/5 + (1001*z)/5 - 40];
65+
66+
%Powell's GradF
67+
g=[2*x + 20*y + 40*(x - z)^3
68+
20*x + 200*y - 4*(2*w - y)^3
69+
10*w - 10*z + 8*(2*w - y)^3
70+
10*z - 10*w - 40*(x - z)^3];
71+
72+
function h = HF(x,y,w,z)
73+
%Wood's Hessian
74+
%h=[1200*x^2 - 400*y + 2, -400*x, 0, 0
75+
% -400*x, 1101/5, 0, 99/5
76+
% 0, 0, 1080*w^2 - 360*z + 2, -360*w
77+
% 0, 99/5, -360*w, 1001/5];
78+
79+
%Powell's Hessian
80+
h=[120*(x - z)^2 + 2, 20, 0, -120*(x - z)^2
81+
20, 12*(2*w - y)^2 + 200, -24*(2*w - y)^2, 0
82+
0, -24*(2*w - y)^2, 48*(2*w - y)^2 + 10, -10
83+
-120*(x - z)^2, 0, -10, 120*(x - z)^2 + 10];
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
syms x1 x2;
2+
assume(x1, 'real');
3+
assume(x2, 'real');
4+
tic
5+
6+
f = x1^2 - 2*x1*x2^2 + x2^4 - x2^5; %obj function (c)
7+
%f = 8*x1^2 + 3*x1*x2 + 7*x2^2 - 25*x1 +31*x2 - 29; %obj function (a)
8+
%f = x1^2 + x1*x2 + 1.5*x2^2 - 2*log(x1) - log(x2); %obj function (b)
9+
10+
%Calculate the Gradient of the objective function
11+
GradF = sym(zeros(2,1));
12+
GradF = [diff(f, x1); diff(f, x2)];
13+
14+
%Calculate the Hessian Matrix of the objective function
15+
HF = sym(zeros(2,2));
16+
HF = hessian(f, [x1,x2]);
17+
18+
%Solve GradF = 0 to find possible local minimizers/maximizers
19+
X = solve([diff(f, x1) == 0, diff(f, x2) == 0], [x1, x2]);
20+
sizeX = size(X.x1);
21+
22+
%Store the solutions of GradF = 0
23+
solutions = sym(zeros(sizeX(1),2));
24+
for i = 1:sizeX(1)
25+
solutions(i,1) = X.x1(i);
26+
solutions(i,2) = X.x2(i);
27+
end
28+
29+
%For every solution do the following..
30+
for i = 1:sizeX(1)
31+
32+
flag_min = false;
33+
flag_max = false;
34+
35+
%Calculate HF_star on the solution of GradF=0 that is being
36+
%investigated
37+
HF_star = subs(HF,{x1,x2}, {solutions(i,1), solutions(i,2)});
38+
39+
%Calculate the det numbers of HF_star
40+
D1_star = HF_star(1,1);
41+
D2_star = det(HF_star);
42+
43+
%Check the sign of the determinants calculated to make an assumption as
44+
%whether the point investigated is local min/max or nothing at all
45+
if D1_star > 0 && D2_star > 0
46+
fprintf('[%d, %d] is a local minimizer. \n', solutions(i,1), solutions(i,2));
47+
flag_min = true;
48+
elseif D1_star < 0 && D2_star >0
49+
fprintf('[%d, %d] is a local maximizer. \n', solutions(i,1), solutions(i,2));
50+
flag_max = true;
51+
elseif D1_star == 0 || D2_star == 0
52+
fprintf('Det value is found to be 0. This method cannot be used. \n')
53+
else
54+
fprintf('[%d, %d] is not a local minimizer/maximizer. \n', solutions(i,1), solutions(i,2));
55+
end
56+
57+
%if a local min was found, check if it is also a global min
58+
if flag_min == true
59+
f_min = subs(f, {x1,x2}, {solutions(i,1), solutions(i,2)});
60+
61+
if isAlways( f >= f_min, 'Unknown', 'false' ) == 0
62+
fprintf('[%d, %d] is not a global minimizer. \n', solutions(i,1), solutions(i,2));
63+
else
64+
fprintf('[%d, %d] is a global minimizer. \n', solutions(i,1), solutions(i,2));
65+
end
66+
%if a local max was found, check if it is also a global max
67+
elseif flag_max == true
68+
f_max = subs(f, {x1,x2}, {solutions(i,1), solutions(i,2)});
69+
70+
if isAlways( f <= f_max, 'Unknown', 'false' ) == 0
71+
fprintf('[%d, %d] is not a global maximizer. \n', solutions(i,1), solutions(i,2));
72+
else
73+
fprintf('[%d, %d] is a global maximizer. \n', solutions(i,1), solutions(i,2));
74+
end
75+
end
76+
end
77+
78+
timeElapsed = toc;
79+
fprintf('The elapsed time was: %f \n', timeElapsed);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
syms x1 x2 x3;
2+
assume(x1, 'real');
3+
assume(x2, 'real');
4+
assume(x3, 'real');
5+
tic
6+
7+
f= x1^2 + 2*x2^2 + 5*x3^2 - 2*x1*x2 - 4*x2*x3 -2*x3; %obj function (d)
8+
9+
%Calculate the Gradient of the objective function
10+
GradF = sym(zeros(3,1));
11+
GradF = [diff(f, x1); diff(f, x2); diff(f, x3)];
12+
13+
%Calculate the Hessian Matrix of the objective function
14+
HF = sym(zeros(3,3));
15+
HF = hessian(f,[x1,x2,x3]);
16+
17+
%Solve GradF = 0 to find possible local minimizers/maximizers
18+
X = solve([diff(f, x1) == 0, diff(f, x2) == 0, diff(f, x3) == 0], [x1, x2, x3]);
19+
sizeX = size(X.x1);
20+
21+
%Store the solutions of GradF = 0
22+
solutions = sym(zeros(sizeX(1),3));
23+
for i = 1:sizeX(1)
24+
solutions(i,1) = X.x1(i);
25+
solutions(i,2) = X.x2(i);
26+
solutions(i,3) = X.x3(i);
27+
end
28+
29+
30+
%For every solution do the following..
31+
for i = 1:sizeX(1)
32+
33+
flag_min = false;
34+
flag_max = false;
35+
36+
%Calculate HF_star on the solution of GradF=0 that is being
37+
%investigated
38+
HF_star = subs(HF,{x1,x2,x3}, {solutions(i,1), solutions(i,2), solutions(i,3)});
39+
40+
%Calculate the det numbers of HF_star
41+
D1_star = HF_star(1,1);
42+
D2_star = det(HF_star(1:2,1:2));
43+
D3_star = det(HF_star);
44+
45+
%Check the sign of the determinants calculated to make an assumption as
46+
%whether the point investigated is local min/max or nothing at all
47+
if D1_star > 0 && D2_star > 0 && D3_star > 0
48+
fprintf('[%d, %d, %d] is a local minimizer. \n', solutions(i,1), solutions(i,2), solutions(i,3));
49+
flag_min = true;
50+
elseif D1_star < 0 && D2_star > 0 && D3_star < 0
51+
fprintf('[%d, %d, %d] is a local maximizer. \n', solutions(i,1), solutions(i,2), solutions(i,3));
52+
flag_max = true;
53+
elseif D1_star == 0 || D2_star == 0 || D3_star == 0
54+
fprintf('Det value is found to be 0. This method cannot be used. \n')
55+
else
56+
fprintf('[%d, %d, %d] is not a local minimizer/maximizer. \n', solutions(i,1), solutions(i,2), solutions(i,3));
57+
end
58+
59+
%if a local min was found, check if it is also a global min
60+
if flag_min == true
61+
f_min = subs(f, {x1,x2,x3}, {solutions(i,1), solutions(i,2), solutions(i,3)});
62+
63+
if isAlways( f >= f_min, 'Unknown', 'false' ) == 0
64+
fprintf('[%d, %d, %d] is not a global minimizer. \n', solutions(i,1), solutions(i,2), solutions(i,3));
65+
else
66+
fprintf('[%d, %d, %d] is a global minimizer. \n', solutions(i,1), solutions(i,2), solutions(i,3));
67+
end
68+
%if a local max was found, check if it is also a global max
69+
elseif flag_max == true
70+
f_max = subs(f, {x1,x2,x3}, {solutions(i,1), solutions(i,2),solutions(i,3)});
71+
72+
if isAlways( f <= f_max, 'Unknown', 'false' ) == 0
73+
fprintf('[%d, %d, %d] is not a global maximizer. \n', solutions(i,1), solutions(i,2), solutions(i,3));
74+
else
75+
fprintf('[%d, %d, %d] is a global maximizer. \n', solutions(i,1), solutions(i,2), solutions(i,3));
76+
end
77+
end
78+
end
79+
80+
timeElapsed = toc;
81+
fprintf('The elapsed time was: %f \n', timeElapsed);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy