Find KKT solution to a given problem
111 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
采纳的回答
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);
% Display results
disp(x) % value of x at the solution
disp(-fun(x)); % value of the function you wanted to maximize
disp(A*x' <= b) % check constraints
disp(lambda) % display Lagrange multipliers
Hope this helps!
6 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!