Solving an optimization problem both graphically and numerically

9 views (last 30 days)
Here are the problems, along with the solutions.
Problem 1
--
[x1, x2] = meshgrid(-3:.1:3);
z = -x1 .^ 2 - x2;
i = find(x1 .^ 2 + x2 .^ 2 > 9); z(i) = NaN;
i = find(x1 + x2 > 1); z(i) = NaN;
surf(x1, x2, z); shading interp
Somehow from the graph we can see that solutions are x_1 = 0, x_2 = −3, and the maximum value of the function is 3.
Problem 2
--
[x1, x2] = meshgrid(0:0.02:1, 1:0.02:2);
z = x1 .^ 3 + x2 .^2 + 4 * x1 + 4;
ii = find(x1 - x2 + 2 < 0); z(ii) = NaN;
ii = find(-x1 .^ 2 + x2 - 1 < 0); z(ii) = NaN;
ii = find(x1 < 0); z(ii) = NaN;
ii = find(x2 < 0); z(ii) = NaN;
surf(x1, x2, z); shading interp
Somehow we can see that x_1 = 0, x_2 = 1.
Numerical solution:
function [c, ce] = exc6f1(x)
ce = [];
c = [x(1)^2 - x(2) + 1];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
f = @(x) x(1) ^ 3 + x(2) ^ 2 + 4 * x(1) + 4;
A = [-1 1]; B = 2; Aeq = []; Beq = []; xm = [0;0];
x = fmincon(f, [0; 1], A, B, Aeq, Beq, xm, [], 'exc6f1')
My questions are:
1. How to determine arguments for the meshgrid function? For the first problem, my book says "from the given constraints, the initial square region [−3, 3] can be selected and grids can be made". Can you explain it step-by-step (for both problems)?
2. How do we read solutions from the graph?
3. How do we determine initial value for the numerical solution? I saw several different functions having this kind of argument (e.g. fsolve, ode45), but I don't know what to put here.

Accepted Answer

Matt J
Matt J on 10 Oct 2018
Edited: Matt J on 10 Oct 2018
1. How to determine arguments for the meshgrid function? For the first problem, my book says "from the given constraints, the initial square region [−3, 3] can be selected and grids can be made". Can you explain it step-by-step (for both problems)?
In problem 1, the first constraint says that (x1,x2) are bounded to a circle of radius 3. So by choosing a square region that covers this circle, you are certain to see at least the entire feasible set in the plot. In problem 2, you can analyze the feasible region more easily by re-arranging the constraints as
1+x1^2 <= x2 <= x1+2
In other words, x2 is lower bounded by a quadratic function of x1 and upper bounded by a linear function of x1. If you plot these as a function of x1, or solve the equation 1+x1^2=x1+2 you will see that 0<=x1<=1 and 1<=x2<=2 are the only intervals where the linear bound lies above the quadratic bound, so choosing the meshgrid to cover these intervals again ensures that the plot will view the entire feasible set.
2. How do we read solutions from the graph?
The surface will reach a maximum or minimum at the solution, so just observe the highest peak or lowest valley on the surface plot. Keep in mind though that this is only an approximate solution. The plot only shows the values of the objective function at the discrete points that you've sampled in the meshgrid. In general, you can't expect the sample points to hit the true solution exactly, except in simple exercises like these that have been set up to have nice integer solutions.
3. How do we determine initial value for the numerical solution?
The idea is to initialize as close to the solution as you can. In general, there is no systematic way to choose this point. Insights specific to the problem must be used. However, in this case, your surface plots let you see approximately where the solution lies, so you can initialize somewhere close to that.
  6 Comments
Nikola Kostic
Nikola Kostic on 13 Oct 2018
For the second problem, I wouldn't really say that 0 <= x1 <= 1 and 1 <= x2 <= 2. I would more likely say that 0 <= x1 <= 1.618 and 1 <= x2 <= 3.618?
Matt J
Matt J on 15 Oct 2018
Yes, I think you're right. But you can also see from the objective function that it is monotonic in x2 and so if you only care about seeing the minimum, the sub-region they gave is enough.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 10 Oct 2018
Edited: Matt J on 10 Oct 2018
2. How do we read solutions from the graph?
Note that you don't actually have to read the solution from the graph. You can also compute it from x1,x2, and z.
optval=max(z(~isnan(z))); %or min()
optlocs=(z==optval);
solutions=[x1(optlocs), x2(optlocs)];

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!