How do I minimize a function graphically?
4 次查看(过去 30 天)
显示 更早的评论
I am trying to minimize f (x1, x2) = (x1 − 1)^2 + (x2 − 5)^2
subject to
−x1^2 + x2 ≤ 4
−(x1 − 2)^2 + x2 ≤ 3
My MATLAB code is below:
clc
clear all
%
x = -0:10;
g1 = -x(1)^2+x(2)-4;
g2 = -(x(1)-2)^2+x(2)-3;
optim = (x(1)-1)^2+(x(2)-5)^2;
%
xlim([0 10])
ylim([0 10])
%
plot(x,g1,x,g2,x,optim,'--')
grid on
grid minor
xlabel('x')
ylabel('y')
0 个评论
回答(1 个)
Sulaymon Eshkabilov
2021-11-10
In your code, your variables are x1 and x2 and NOT x. Moreover, g1 and g2 are incorrectly defined.
In this exercise, you should use fmincon() fcn of Optimization toolbox. See this doc: https://www.mathworks.com/help/optim/ug/minimization-with-bound-constraints-and-banded-preconditioner.html
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Nonlinear Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!