Maximize objective function using fmincon with a limit
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Angel Ani
le 27 Juin 2021
Commenté : William Rose
le 29 Juin 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 commentaires
Réponse acceptée
William Rose
le 27 Juin 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 commentaires
William Rose
le 29 Juin 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.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Elementary Math dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!