lagrange multiplier

 avatar
unknown
matlab
2 years ago
699 B
4
Indexable
clc
clear all
format compact
syms x y lam real 
f= input('Enter f(x,y) to be extremized : ');
g= input('Enter the constraint function g(x,y) : '); 
F=f-lam*g; 
Fd=jacobian(F,[x y lam]); 
[ax,ay,alam]=solve(Fd,x,y,lam);  
ax=double(ax); ay=double(ay); 
T = subs(f,{x,y},{ax,ay});
T=double(T); epxl=min(ax); 
epxr=max(ax);epyl=min(ax);  
epyu=max(ax)
D=[epxl-0.5 epxr+0.5 epyl-0.5 epyu+0.5] 
ezcontourf(f,D)
hold on 
h = ezplot(g,D);
set(h,'Color',[1,0.7,0.9])
for i = 1:length(T); 
    fprintf('The critical point (x,y) is (%1.3f,%1.3f).',ax(i),ay(i)) 
    fprintf('The value of the function is %1.3f\n',T(i)) 
    plot(ax(i),ay(i),'k.','markersize',15)
end 
TT=sort(T); 
f_min=TT(1); 
f_max=TT(end);
Editor is loading...