Maximize objective function using fmincon with a limit
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Angel Ani
am 27 Jun. 2021
Kommentiert: William Rose
am 29 Jun. 2021
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 Kommentare
Akzeptierte Antwort
William Rose
am 27 Jun. 2021
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 Kommentare
William Rose
am 29 Jun. 2021
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Elementary Math finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!