Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

% Define the function

f = @(x, y) 25*x.^2 + 12*y.^2 - 20*x.*y + 120*y;

% Define the range of (x, y)


x = linspace(-15, 10, 100);
y = linspace(-15, 10, 100);
[X, Y] = meshgrid(x, y);

% Evaluate the function over the grid


Z = f(X, Y);

% Plot the contours of the function


contour(X, Y, Z, 50); % You can adjust the number of contour levels

hold on;

% Plot the feasible region x + y >= 0


fplot(@(x) -x, [-15, 10], 'r', 'LineWidth', 2); % Plot x + y = 0

% Find minimum within the feasible region


options = optimoptions('fmincon', 'Display', 'off');
[x_min, f_min] = fmincon(@(x) f(x(1), x(2)), [0, 0], [], [], [1, 1], 0,
[], [], [], options);

% Plot the minimum point


plot(x_min(1), x_min(2), 'go', 'MarkerSize', 10); % Plot minimum point
text(x_min(1), x_min(2), ['Minima: (', num2str(x_min(1)), ', ',
num2str(x_min(2)), ')'], 'VerticalAlignment', 'bottom');

hold off;

xlabel('x');
ylabel('y');
title('Contours of f(x, y) = 25x^2 + 12y^2 - 20xy + 120y with Feasible
Region');

% Add legend
legend('Contours', 'Feasible Region (x + y \geq 0)', 'Minima',
'Location', 'northwest');

1
2

You might also like