Session7 2

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

Lagrange Multipliers

%% MatLab code for Lagrange%s multiplier method for two variables

% Clear command window, workspace and close figure windows


clc; clear; close all;

% Symbolic variables
syms x y L real;

% Input the function f(x, y)


f = input('Enter the function f(x, y):');
% Input the constrained function g(x, y)
g = input('Enter the constrained function g(x, y):');

% Form the Lagrangian function F = f + L * g


F = f+L*g;
% Compute the gradient of F
gradF = jacobian(F, [x y]);
% Solve for the stationary points and Lagrange multiplier
S = solve(g==0, gradF(1)==0, gradF(2)==0, 'Real', true); % Solving only for Real x & y

% Extract the coordinates of the stationary points


St_pts = [S.x, S.y];
h1 = St_pts(:, 1);
h2 = St_pts(:, 2);
X = double(h1);
Y = double(h2);
STP = table(X, Y);

% Display the stationary points


disp('Stationary points are:');
disp(STP);
% Define the range for plotting
range = double([min(S.x)-3 max(S.x)+3 min(S.y)-3 max(S.y)+3]);

% Compute the function values at the stationary points


[n, m] = size(St_pts);
F = zeros(n, 1);
for i = 1:n
F(i) = double(subs(f, {x, y}, {S.x(i), S.y(i)}));
end
% Determine the maximum and minimum values of f under the given constraint
if n > 1
F_max = max(F);
disp(['The maximum value of f under the given constraint g is: ' num2str(F_max)]);
F_min = min(F);
disp(['The minimum value of f under the given constraint g is: ' num2str(F_min)]);
else
disp(['The extremum value of f under the given constraint g is: ' num2str(F)]);
end

% Plot the function and constraint surface


fmesh(f, range);
hold on;
h = ezplot(g, range);
tmp = get(h, 'contourMatrix'); % Retrieve the contour data from the constraint plot
xdt = tmp(1, 2:end); % Extract x-coordinates of contour points, excluding the first
point
ydt = tmp(2, 2:end); % Extract y-coordinates of contour points, excluding the first
point
zdt = double(subs(f, {x, y}, {xdt, ydt}));
plot3(xdt, ydt, zdt, '-r', 'LineWidth', 2);
axis(range);

% Plot the stationary points


for i = 1:numel(X)
plot3(S.x(i), S.y(i), F(i), '*k', 'MarkerSize', 20);
end

hold off;
title('Constrained Maxima/Minima’);

======================================================================================
%% MatLab code for Lagrange’s multiplier method for three variables:

clear % Clear all variables


clc % Clear the command window
syms x y z L % Declare symbolic variables

% Prompt user to enter the function f(x,y,z)


f = input('Enter the function f(x,y,z): ');

% Prompt user to enter the constraint function g(x,y,z)


g = input('Enter the constraint function g(x,y,z): ');
F = f + L*g; % Define the Lagrangian function F
gradF = jacobian(F,[x,y,z]); % Compute the gradient of F

% Solve the equations gradF(1)=0, gradF(2)=0, gradF(3)=0, and g=0 for x, y, z


S = solve(gradF(1)==0, gradF(2)==0, gradF(3)==0, g==0);
% Store the stationary points in st_pts matrix
st_pts = double([S.x,S.y,S.z]);

% Extract the x, y, z coordinates of the stationary points


h1 = st_pts(:,1);
h2 = st_pts(:,2);
h3 = st_pts(:,3);
X = h1; % Column vector of x coordinates
Y = h2; % Column vector of y coordinates
Z = h3; % Column vector of z coordinates

STP = table(X, Y, Z); % Create a table to display the stationary points


disp('Stationary points are:')
disp(STP);

[n, m] = size(st_pts); % Get the dimensions of st_pts matrix


% Calculate the values of f at all the stationary points
for i = 1:n
F(i) = subs(f, {x, y, z}, {S.x(i), S.y(i), S.z(i)});
end

if n > 1
F_max = max(F); % Find the maximum value of f
disp(['The maximum value of f under the given constraint g is: '
num2str(double(F_max))]);
F_min = min(F); % Find the minimum value of f
disp(['The minimum value of f under the given constraint g is: '
num2str(double(F_min))]);
else
disp(['The extremum value of f under the given constraint g is: '
num2str(double(F))]);
end

Practice Problems:
1. Find the extreme values of the func on f (x, y) = 3x + 4y on the circle x 2 + y 2 = 1.
x2 y2
2. Find the extreme values of the func on f (x, y) = x y on the ellipse + = 1.
8 2
3. Find the extreme values of the func on f (x, y, z) = x y 2 z 3 on the plane x + y + z = 24.
ti
ti
ti

You might also like