How to plot the optimal point of an objective function

10 views (last 30 days)
I want to maximise the optimization model ∑F with respect to x,y.But this function has different values for two conditions:
F=y*Eb-x*Es+c*(Es-Eb) ; Es≥Eb
F=y*Eb-x*Es+d*(Es-Eb) ; Es<Eb
subject to : c ≤ x , y ≤ d
The parameters are Eb,Es,c & d can choose random variables.
I want to plot the optimal values of this function on graph
  2 Comments
Alan Weiss
Alan Weiss on 1 Mar 2022
I do not understand your question. Do the parameters "choose random variables" before optimization or during optimization? If before, then you can take the values and try to solve the problem. If during, then I have no idea what the problem means.
Alan Weiss
MATLAB mathematical toolbox documentation
jessupj
jessupj on 1 Mar 2022
Edited: jessupj on 1 Mar 2022
Also, the question says something about plotting. you have 4 parameters so you need to clarify whether you're trying to plot the optmial point in the 4d parameter space (a 2d function F(x,y | optimal_parameters) ) or the optimal point (x,y) as a function of the parameters.
But as long as i'm making a comment, you can define a single objective funtion:
F= y*Eb-x*Es+( c*(Es>=Eb) + d*(Es<Eb) ) *(Es-Eb)
that might be easier to call from e.g. fmincon subject to the other nonlinear constraints. (And, of course, if you're tring to find a maximum, you'd try to minimize -1*F)

Sign in to comment.

Answers (1)

Binaya
Binaya on 18 Jan 2024
Edited: Binaya on 18 Jan 2024
Hi Ancy
I see that you would like to maximize the optimization model w.r.t. "x" and "y". where "Eb", "Es", "c" and "d" are parameters to the optimization that is fixed before the optimization starts.
You can follow the following steps to maximize the optimization model and plot the optimal values on a graph:
  1. Initialize "Eb", "Es", "c" and "d" to a random value using "rand".
  2. Define the objective function, inequality and equality constraints. Here is an example code:
objectiveFunction = @(vars) -piecewiseObjective(vars, Eb, Es, c, d);
constraintsFunction = @(vars) constraints(vars, c, d);
function F = piecewiseObjective(vars, Eb, Es, c, d)
x = vars(1);
y = vars(2);
if Es >= Eb
F = y * Eb - x * Es + c * (Es - Eb);
else
F = y * Eb - x * Es + d * (Es - Eb);
end
end
function [c, ceq] = constraints(vars, c, d)
x = vars(1);
y = vars(2);
% Inequality constraints (c(x) <= 0)
c = [c - x; y - d];
% No equality constraints
ceq = [];
end
3. Setup the options for optimization model using "optimoptions" according to the requirement.
4. Call the "fmincon" function for optimization using the objective functions, constraints and options defined above.
5. Create a grid of "x" and "y" values using "meshgrid" for plotting.
6. Find the value of the objective function over the mesh grid:
FGrid = arrayfun(@(x, y) piecewiseObjective([x, y], Eb, Es, c, d), xGrid, yGrid);
7. Plot a 3d plot using "xGrid", "yGrid" and "FGrid".
8. Plot the optimum point which is returned by "fmincon" in the existing figure.
Hope this helps

Categories

Find more on Problem-Based Optimization Setup in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!