How do I minimize a function graphically?

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')

Answers (1)

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

Categories

Asked:

on 10 Nov 2021

Community Treasure Hunt

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

Start Hunting!