Find KKT solution to a given problem

조회 수: 138 (최근 30일)
Ezra
Ezra 2024년 2월 14일
이동: John D'Errico 2024년 2월 14일
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)

채택된 답변

Sai Teja G
Sai Teja G 2024년 2월 14일
편집: Sai Teja G 2024년 2월 14일
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
Star Strider
Star Strider 2024년 2월 14일
Try:
disp(lambda.upper)
Ezra
Ezra 2024년 2월 14일
이동: John D'Errico 2024년 2월 14일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

제품


릴리스

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by