clear all
close all


set(0, 'DefaultLineLineWidth', 1, ...
    'DefaultLineMarkerSize', 6,...
    'DefaultAxesFontSize', 18, ...
    'DefaultAxesTickLabelInterpreter', 'latex',...
    'DefaultFigurePaperOrientation', 'portrait',...
    'DefaultFigurePaperUnits', 'inches',...
    'DefaultFigurePaperSize', [6, 4.5],...
    'DefaultFigurePaperPosition', [0,0,6,4.5],...
    'DefaultLegendInterpreter', 'latex',...
    'DefaultTextInterpreter', 'latex',...
    'DefaultFigureRenderer', 'painters')

% Set up the grid
[x, y] = meshgrid(-3:.05:3, -3:.05:3);  % Tighter domain to match radius ~2.24
x = x';
y = y';

% Compute the objective function
z = x.^2 .* exp(y);

% Define custom contour levels
custom_levels = [.5 1 2 4 7 12.3 20 36 60];

% Plot contours
close all;
figure;
set(gcf, 'Position', [10, 10, 800, 800]);
    

subplot(1,2,1) 
contour(x, y, z, custom_levels, 'ShowText', 'on', 'LineWidth', 3);
hold on;

% Plot constraint: x^2 + y^2 = 5
theta = linspace(0, 2*pi, 300);
r = sqrt(5);
xc = r * cos(theta);
yc = r * sin(theta);
plot(xc, yc, 'k', 'LineWidth', 2, 'LineStyle','--');

% Axes lines
xline(0, 'k');
yline(0, 'k');

% Axis labels
text(-.1, 1, '$x_2$', 'Interpreter', 'latex', 'FontSize', 20);
text(.3, 0, '$x_1$', 'Interpreter', 'latex', 'FontSize', 20);

grid

% Formatting
axis equal;
% title('$F(x,y) = x^2 e^y$ with constraint $x^2 + y^2 = 5$', 'Interpreter','latex', 'FontSize', 20);
 title('Top View', 'Interpreter','latex', 'FontSize', 20);

% Plot optimal points as stars
x_opt = sqrt(2 * (-1 + sqrt(6)));
y_opt = -1 + sqrt(6);
plot(x_opt, y_opt, 'kp', 'MarkerSize', 12, 'MarkerFaceColor', 'k');   % Star at (+x, y)
plot(-x_opt, y_opt, 'kp', 'MarkerSize', 12, 'MarkerFaceColor', 'k');  % Star at (-x, y)

    % x0=10;
    % y0=10;
    % width=500;
    % height=500;
    % set(gcf,'position',[x0,y0,width,height])
    % tightfig
%saveas(gcf,'lect2_lagrange.pdf')

%
subplot(1,2,2) 
% Define grid
[x, y] = meshgrid(-2.2:0.25:2.2, -2.2:0.25:2.2);
F = x.^2 .*exp(y);

% Plot the surface
meshc(x, y, F);
colormap turbo
hold on;
set(gcf, 'Position', [46 107 767 682]);

% Now plot the constraint curve g(x, y) = 5
theta = linspace(0, 2*pi, 400);
r = sqrt(5);
xc = r * cos(theta);
yc = r * sin(theta);
zc = xc.^2 .*exp(yc);

plot3(xc, yc, zc+.1, 'k', 'LineWidth', 2);  % Thick black constraint curve on surface

% Labels
xlabel('$x$'); ylabel('$y$'); zlabel('$F(x,y)$');
title('$F(x,y) = x^2 e^y$ with constraint curve $x^2 + y^2 = 5$');
view(-30, 30);  % nice 3D angle
 title('Side View', 'Interpreter','latex', 'FontSize', 20);


% Find maximum on the constraint
[~, maxIdx] = max(zc);
x_opt = xc(maxIdx);
y_opt = yc(maxIdx);
z_opt = zc(maxIdx);

% Plot optimal point as red star
plot3(x_opt,  y_opt, z_opt+0.5, 'kp', 'MarkerSize', 12, 'LineWidth', 2);
plot3(-x_opt, y_opt, z_opt+0.5, 'kp', 'MarkerSize', 12, 'LineWidth', 2);

    x0=10;
    y0=10;
    width=1000;
    height=500;
    set(gcf,'position',[x0,y0,width,height])
    tightfig
saveas(gcf,'lect2_lagrange.pdf')

%% THE END