Azzera filtri
Azzera filtri

Find KKT solution to a given problem

47 visualizzazioni (ultimi 30 giorni)
Ezra
Ezra il 14 Feb 2024
Spostato: John D'Errico il 14 Feb 2024
I'm trying to solve kkt problem with matlab but I'm having problem with my code, please, I want some one to assist me in correcting errors in my codes
problem:
Maximize R= 208*x1**0.323168*x2**0.172084*x3**0.085998*x4**0.188794*x5**0.229956
subject to:
2.8 * x1 + 22 * x2 + 4 * x3 + 0.272 * x4 + x5 <= 492
22 * x2 + 4 * x3 <= 212
x5 <= 20
Bounds
x1= (lb = 40, ub = 80)
x2= (lb = 5, ub = 12)
x3= (lb = 4, ub = 11)
x4= (lb = 20, ub = 70)
x5= (lb = 7, ub = 20)
X0 = [41,6,5,22,8]
Proposed solution:
fun = @(x)-208*x(1)^0.323168*x(2)^0.172084*x(3)^0.085998*x(4)^0.188794*x(5)^0.229956;
x0=[41,6,5,22,8]; %initial guess for the solution
%express first constraint in the form A*x<=b:
Aeq=[41,6,5,22,8];
beq = 1;
A=[2.8,22,4,0.272]; b=492;
C=[0,22,4,0,0]; d=212;
E=[0,0,0,0,1]; f=20;
%express constraints 2 and 3 by defining vectors lb and ub:
lb=[40,5,4,20,7]; %no lower blound constraints
ub=[80, 12,11,70,20]; %upper bounds on x(4),x(5)
[x,fval,exitflag,output,lambda]=fmincon(fun,x0,A,b,[],[],lb,ub);
disp(x) %value of x at the solution
disp(-fun(x)); %value of the function you wanted to maximize
disp(A*x') %check constraint 1
disp(lambda)

Risposta accettata

Sai Teja G
Sai Teja G il 14 Feb 2024
Modificato: Sai Teja G il 14 Feb 2024
Hi Ezra,
You can refer to the below code to solve the problem.
% Objective function
fun = @(x)-208*x(1)^0.323168*x(2)^0.172084*x(3)^0.085998*x(4)^0.188794*x(5)^0.229956;
% Initial guess for the solution
x0 = [41, 6, 5, 22, 8];
% Express first constraint in the form A*x <= b
A = [2.8, 22, 4, 0.272, 1]; % Add the missing element for x5
b = 492;
% Combine the second and third constraints into A and b
A = [A; 0, 22, 4, 0, 0; 0, 0, 0, 0, 1]; % Add the second and third constraints
b = [b; 212; 20];
% Lower and upper bounds
lb = [40, 5, 4, 20, 7]; % Lower bounds
ub = [80, 12, 11, 70, 20]; % Upper bounds
% Run fmincon
[x, fval, exitflag, output, lambda] = fmincon(fun, x0, A, b, [], [], lb, ub);
Local minimum found that satisfies the constraints. Optimization completed because the objective function is non-decreasing in feasible directions, to within the value of the optimality tolerance, and constraints are satisfied to within the value of the constraint tolerance.
% Display results
disp(x) % value of x at the solution
80.0000 7.6364 11.0000 70.0000 20.0000
disp(-fun(x)); % value of the function you wanted to maximize
6.6388e+03
disp(A*x' <= b) % check constraints
1 1 1
disp(lambda) % display Lagrange multipliers
eqlin: [0×1 double] eqnonlin: [0×1 double] ineqlin: [3×1 double] lower: [5×1 double] upper: [5×1 double] ineqnonlin: [0×1 double]
Hope this helps!
  6 Commenti
Star Strider
Star Strider il 14 Feb 2024
Try:
disp(lambda.upper)
Ezra
Ezra il 14 Feb 2024
Spostato: John D'Errico il 14 Feb 2024
Thank you all for your effort the matter of lamda is solved, it was an error in spelling from me. Please sorry for taking your time.
merci beaucoup

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Programming Utilities in Help Center e File Exchange

Prodotti


Release

R2016b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by