Maximize objective function using fmincon with a limit
조회 수: 18 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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
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:
- Line 2 is impossible: P cannot be greater than .4 and less than .3.
- Equation (U*y1 - P*y1) + (P*y2 - V*y2) does not constrain P, because is has no value on the right or left side.
- There is no "x" in the equation above, and U,V,y1,y2 are specified (in the code), so there is nothing to adjust.
- 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 Exchange에서 Discrete Math에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!