0% found this document useful (0 votes)
8 views

MATLAB8

The document provides MATLAB code to find the maximum and minimum values of functions using Lagrange's multiplier method. It gives examples of using the code to find the extrema of functions subject to equality constraints. Practice problems are also included for the reader to try.

Uploaded by

lazzygirl2002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

MATLAB8

The document provides MATLAB code to find the maximum and minimum values of functions using Lagrange's multiplier method. It gives examples of using the code to find the extrema of functions subject to equality constraints. Practice problems are also included for the reader to try.

Uploaded by

lazzygirl2002
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

20BCE1629

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 =

'The value of the function is 0'

ans =

'The point (x,y) is(1.414214e+00,1)'

ans =

'The value of the function is 1'

ans =

'The point (x,y) is(-1.414214e+00,1)'

ans =

'The value of the function is 1'

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 =

'The point (x,y) is(25,50)'

ans =

'The value of the function is 2600'

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 =

'The point (x,y) is(-1,-1)'

ans =

'The value of the function is 2'

ans =

'The point (x,y) is(1,1)'

ans =

'The value of the function is 2'


Figures:-
2. Command Window

Enter the function to be maximized in terms of x and y:


4*x+6*y
Enter the constraint function in terms of x and y:
x^2+y^2-1

T =

-2*13^(1/2)
2*13^(1/2)

ans =

'The point (x,y) is(-5.547002e-01,-8.320503e-01)'

ans =

'The value of the function is -7.211103e+00'

ans =

'The point (x,y) is(5.547002e-01,8.320503e-01)'

ans =

'The value of the function is 7.211103e+00'


Figure:-

You might also like