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

Matlab 6

Uploaded by

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

Matlab 6

Uploaded by

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

Name: Fawaz Khan

Reg.no:24BLC1355

MATLAB ASSIGNMENT-6

AIM:

Describe an optimization involving an objective function in two unknowns


and a single constraint. Write down the objective function and the
constraints.

Solve it using MATLAB via

(1)Discriminant method and (2) Lagrange Multipliers method .

PROGRAM:

(1) clc
clear
syms x y

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


fx=diff(f,x); fy=diff(f,y);
[ax,ay]=solve(fx,fy);
ax=double(ax);ay=double(ay);
fxx=diff(fx,x); fxy=diff(fx,y); fyy=diff(fy,y);D=fxx*fyy-
fxy^2;
figure
fsurf(f);
legstr={'Function Plot'};

for i=1:size(ax)
T1=D(ax(i),ay(i));
T2=fxx(ax(i),ay(i));
T3=f(ax(i),ay(i));
if(double(T1)==0)
sprintf('At (%f,%f) further investigation is
required',ax(i),ay(i))
legstr=[legstr,{'Case of Further investigation'}];
mkr='ko';
elseif (double(T1)<0)
sprintf('The point (%f,%f) is a saddle point', ax(i),ay(i))
legstr=[legstr,{'Saddle Point'}];
mkr='bv';
else
if (double(T2) < 0)
sprintf('The maximum value of the function is f(%f,%f)=%f',
ax(i),ay(i), T3)
legstr=[legstr,{'Maximum value of the function'}];
mkr='g+';
else
sprintf('The minimum value of the function is f(%f,%f)=%f',
ax(i),ay(i), T3)
legstr=[legstr,{'Minimum value of the function'}];
mkr='r*';
end
end
hold on
plot3(ax(i),ay(i),T3,mkr,'Linewidth',4);
end
legend(legstr,'Location','Best');
(2) clc
clearvars
syms x y L
f = input('Enter the function f(x,y): ');
g = input('Enter the constraint function g(x,y): ');
h = f + L*g;
gradh = jacobian(h,[x,y]);
[L,x1,y1] = solve(g,gradh(1),gradh(2),'Real',true);
x1 = double(x1); y1 = double(y1);
xmx = max(x1); xmn = min(x1);
ymx = max(y1); ymn = min(y1);
range = [xmn-3 xmx+3 ymn-3 ymx+3];
ezmesh(f,range);hold on; grid on;
h = ezplot(g,range); set(h,'LineWidth',2);
tmp = get(h,'contourMatrix');
xdt = tmp(1,2:end);
ydt = tmp(2,2:end);
zdt = double(subs(f,{x,y},{xdt,ydt}));
plot3(xdt,ydt,zdt,'-r','LineWidth',2);axis(range);
for i = 1:numel(x1)
G(i) = subs(f,[x,y],[x1(i),y1(i)])
plot3(x1(i),y1(i),G(i),'*k','MarkerSize',20);
end
title('Constrained Maxima/Minima')
OUTPUT:

(1)
(2)
CONCLUSION:

In this assignment, we explored two approaches to solving an

optimization problem with a single constraint using MATLAB: the

standard method of substitution and the Lagrange multipliers method.

MATLAB’s symbolic capabilities made the differentiation and solving

processes straightforward and efficient.

For the Lagrange multipliers method, we used MATLAB’s symbolic

toolbox to define and compute the partial derivatives of the Lagrangian

function. The system of equations derived from these derivatives was

then solved using MATLAB’s solve function, giving us the same optimal

solution.

In conclusion, MATLAB provides a robust and flexible environment for

solving constrained optimization problems. The computational efficiency

and accuracy of MATLAB in handling both symbolic and numeric

calculations make it an ideal tool for optimization problems in

engineering and other applied fields.

You might also like