MATLAB Answers

Maximize objective function using fmincon with a limit

조회 수: 8(최근 30일)
Angel Ani
Angel Ani 2021년 6월 27일
댓글: William Rose 2021년 6월 29일
Please help me with the error in the code; to get the "profit".
Maximize P: (U*y1 - P*y1) + (P*y2 - V*y2)
where 0.4 <= P <= 0.3
f=@x-1*((U*y1 - (x)*y1) + ((x)*y2 - V*y2))
U=2;y1=3;y2=1;V=0.2;
A=[]; q=[]; lb=0.3; ub=0.4;
[ans,ans1]= fmincon(f,A,q,lb,ub);
ans; ans1

채택된 답변

William Rose
William Rose 2021년 6월 27일
Create the following function and save it as file parameterfun.m:
function y=parameterfun(x,U,V,y1,y2)
y=-((U*y1 - x*y1) + (x*y2 - V*y2)); %-profit
Then execute the following code:
U=2;y1=3;y2=1;V=0.2;
f = @(x)parameterfun(x,U,V,y1,y2);
x0=0.35; %initial guess
lb=0.3; ub=0.4;
[x,fval] = fmincon(f,x0,[],[],[],[],lb,ub);
fprintf('x=%.3f, Profit=%.3f\n',x,-fval);
This produces the following output on the console:
x=0.300, Profit=5.200
  댓글 수: 4
William Rose
William Rose 2021년 6월 29일
I see now that the problem statement in your orignal post has issues. You stated the problem as:
Maximize P: (U*y1 - P*y1) + (P*y2 - V*y2),
where 0.4 <= P <= 0.3
The issues are:
  1. Line 2 is impossible: P cannot be greater than .4 and less than .3.
  2. Equation (U*y1 - P*y1) + (P*y2 - V*y2) does not constrain P, because is has no value on the right or left side.
  3. There is no "x" in the equation above, and U,V,y1,y2 are specified (in the code), so there is nothing to adjust.
  4. In maximization problems, one does not constrain the thing being maximized (Profit, in this case). One constrains the adjustable input(s) that determine the profit. Threfore the second line does not make sense, if we are to maximize P.
One possibility is that the original statement should have been
Maximize P: (U*y1 - x*y1) + (x*y2 - V*y2),
where 0.3 <= x <= 0.4.
The problem above is the one which my code solves, and which your originally posted code almost solves.
P at the solution is outside the bounds [0.3, 0.4] because [0.3, 0.4] are bounds for x, not for P.

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

추가 답변(0개)

제품


릴리스

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by