Code
Example 4.29 Arora pg. 128
Step One
Step Two
Step Three
Step Four
Example 4.27 Arora pg. 122
Step One
Step Two
Step Three
Step Four
Modifications
%KKT with 2 variables, 2 linear constraints
with one equality and one
%inequality constraint at once or 1 equality
and no inequality constraint at once or no
%equality and one inequality constraint at
once
clc;
syms x1 x2 g1 h1 u1 v1 s1
%input of a function like (x1 - 1.5).^2 + (x2
- 1.5).^2 for up to two variables x1, x2
fx=input('Enter the function of ''x'' to be
minimized of the form (x - 4).^2 + (y - 6).^2 ');
clc;
C=input('Enter the number of constraints in
the problem, e.g. 10 for one equality and no inequality constraint ');
clc;
if C==01
%constraints input
g1=input('Enter inequality constraint in the form x1+x2-10 ');
clc;
%Lagrange function calculation
L=fx+(u1*(g1+s1.^2));
%gradient conditions
dx1=(diff (L,x1)); dx2=(diff (L,x2)); du1=(diff (L,u1)); ds1=(diff
(L,s1));
%simultaneous solution of gradient conditions
[sols1,solu1,solx1,solx2]=solve(dx1==0,dx2==0,du1==0,ds1==0);
a=zeros(3,4);
for
i=1:3
a(i,1)=solx1(i,1);
a(i,2)=solx2(i,1);
a(i,3)=sols1(i,1);
a(i,4)=solu1(i,1);
end
SET1=a(1,:); SET2=a(2,:); SET3=a(3,:);
%feasibility and non-negativity of Lagrange multiplier check
if
SET1(1,3)>0 || SET1(1,4)>0
disp('Set 1 '); disp ('
x1 x2 s
u'); disp (SET1)
end
if
SET2(1,3)>0 || SET2(1,4)>0
disp('Set 2 '); disp ('
x1 x2 s
u'); disp (SET2)
end
if
SET3(1,3)>0 || SET3(1,4)>0
disp('Set 3 '); disp ('
x1 x2 s
u'); disp (SET3)
end
end
if C==10
%constraints input
h1=input('Enter equality constraint in the form x1+x2-10 ');
clc;
%Lagrange function calculation
L=fx+(v1*h1);
%gradient conditions
dx1=(diff (L,x1)); dx2=(diff (L,x2)); dv1=(diff (L,v1));
%simultaneous solution of gradient conditions
[solv1,solx1,solx2]=solve(dx1==0,dx2==0,dv1==0);
a=zeros(1,3);
a(1,1)=solx1;
a(1,2)=solx2;
a(1,3)=solv1;
disp('Set 1 '); disp ('
x1 x2 v'); disp (a);
end
if C==11
%constraints input
g1=input('Enter inequality constraint in the form x1+x2-10 ');
clc;
h1=input('Enter equality constraint in the form x1+x2-10 ');
clc;
%Lagrange function calculation
L=fx+(v1*h1)+(u1*(g1+s1.^2));
%gradient conditions
dx1=(diff (L,x1)); dx2=(diff (L,x2)); du1=(diff (L,u1)); ds1=(diff
(L,s1)); dv1=(diff (L,v1));
%simultaneous solution of gradient conditions
[sols1,solu1,solv1,solx1,solx2]=solve(dx1==0,dx2==0,du1==0,ds1==0,dv1==0);
a=zeros(3,5);
for
i=1:3
a(i,1)=solx1(i,1);
a(i,2)=solx2(i,1);
a(i,3)=sols1(i,1);
a(i,4)=solu1(i,1);
a(i,5)=solv1(i,1);
end
SET1=a(1,:); SET2=a(2,:); SET3=a(3,:);
%feasibility and non-negativity of Lagrange multiplier check
if
SET1(1,3)>0 || SET1(1,4)>0
disp('Set 1 '); disp ('
x1 x2 s u v'); disp (SET1)
end
if
SET2(1,3)>0 || SET2(1,4)>0
disp('Set 2 '); disp ('
x1 x2 s u v'); disp (SET2)
end
if
SET3(1,3)>0 || SET3(1,4)>0
disp('Set 3 '); disp ('
x1 x2 s
u v'); disp (SET3)
end
end
Example 4.29 Arora pg. 128
Step One
In this step the user enters the objective function:
Command Window
Enter the function of 'x' to be minimized (x1 - 1.5).^2 +
(x2 - 1.5).^2
Step Two
In this step the user specifies the type(s) and quantity of
constraints the objective function is subjected to.
01 means no equality and one inequality constraint, 10 means
one equality and no inequality constraint, 11 means one equality and one
inequality constraint.
Command Window
Enter the number of constraints in the problem, e.g. 10 for
one equality and no inequality constraint 01
Step Three
In this step the user enter the constraint(s).
Command Window
Enter inequality constraint in the form x1+x2-10 x1 + x2 – 2
Step Four
Output of the program; feasible set with candidate minimum
point(s).
Command Window
Set 1
x1 x2
s u
1 1
0 1
Example 4.27 Arora pg. 122
Step One
Command Window
Enter the function of 'x' to be minimized (x1 - 1.5).^2 +
(x2 - 1.5).^2
Step Two
Command Window
Enter the number of constraints in the problem, e.g. 10 for
one equality and no inequality constraint 10
Step Three
Command Window
Enter equality constraint in the form x1+x2-10 x1 + x2 – 2
Step Four
Command Window
Set 1
x1 x2
v
1 1
1
Modifications
Extension to five or even more variables and more
constraints can be made via simple modifications to the code. The structure of
the new code will remain similar to the code mention above, though it will have
many more lines of instructions.
No comments:
Post a Comment