Matlab 6
Matlab 6
Reg.no:24BLC1355
MATLAB ASSIGNMENT-6
AIM:
PROGRAM:
(1) clc
clear
syms x y
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:
then solved using MATLAB’s solve function, giving us the same optimal
solution.