Maximize objective function using fmincon with a limit

4 ビュー (過去 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 件のコメント
Angel Ani
Angel Ani 2021 年 6 月 29 日
Why is the value of profit exceeding the limit?
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 件)

カテゴリ

Help Center および File ExchangeElementary Math についてさらに検索

製品


リリース

R2016a

Community Treasure Hunt

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

Start Hunting!

Translated by