MATLAB8
MATLAB8
Anouska Acharya
MATLAB8
AIM: To write the MATLAB codes to find maximum and minimum in
Lagrange’s multiplier’s method.
Code:-
clc
clear all
syms x y lam real
f= input('Enter the function to be maximized in terms of x and y:');
g= input('Enter the constraint function in terms of x and y:');
[alam,ax,ay]=solve(jacobian(f-lam*g,[x y lam]));
T=subs(f,{x,y},{ax,ay})
for i = 1:1:size(T)
figure
sprintf('The point (x,y) is(%d,%d)',double(ax(i)),double(ay(i)))
sprintf('The value of the function is %d',double(T(i)))
[X1,Y1]=meshgrid(double(ax(i))-3:.2:double(ax(i))+3,double(ay(i))-
3:.2:double(ay(i))+3);
zfun=@(x,y)eval(vectorize(f));
Z1= zfun(X1,Y1);
contour(X1,Y1,Z1,75)
hold on
h=fimplicit(g,[double(ax(i))-3,double(ax(i))+3]);
set(h,'Color',[1,0.7,0.9])
plot(double(ax(i)),double(ay(i)),'r.','markersize',12)
end
Command window
Enter the function to be maximized in terms of x and y:
x^2-y^2
Enter the constraint function in terms of x and y:
2*y-x^2
T =
0
1
1
ans =
'The point (x,y) is(0,0)'
ans =
ans =
ans =
ans =
ans =
Figures
2. Find the extreme values of the function f(x,y)=2x+2xy+y subject to the constraints
2x+y=100
Code
Enter the function to be maximized in terms of x and y:
2*x+2*x*y+y
Enter the constraint function in terms of x and y:
2*x+y-100
T =
2600
ans =
ans =
Figure
Practice Problems
1. Find the extreme values of the function f(x,y)= X^2+y^2 subject
to xy=1
2. Find the extreme values of the function f(x,y)=4x+6y subject to
x^2 +y^2=1
1. Output
Enter the function to be maximized in terms of x and y:
x^2+y^2
Enter the constraint function in terms of x and y:
x*y-1
T =
2
2
ans =
ans =
ans =
ans =
T =
-2*13^(1/2)
2*13^(1/2)
ans =
ans =
ans =
ans =