How do I minimize a function graphically?
Ältere Kommentare anzeigen
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')
Antworten (1)
Sulaymon Eshkabilov
am 10 Nov. 2021
0 Stimmen
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
Kategorien
Mehr zu Problem-Based Optimization Setup finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!