Solving an optimization problem both graphically and numerically
显示 更早的评论
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.
采纳的回答
更多回答(1 个)
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)];
类别
在 帮助中心 和 File Exchange 中查找有关 Linear Least Squares 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
