How do I minimize a function graphically?

1 次查看(过去 30 天)
Javier Garcia
Javier Garcia2021-11-10
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')

回答(1 个)

Sulaymon Eshkabilov
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

Community Treasure Hunt

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

Start Hunting!

Translated by