f=input('nhap ham f(x,y)= '); disp('nhap parabol y(x). vd: y=6+4*x^2 ') phi=input('y = '); hold on L=f+lamda*(phi-y); Lx=diff(L,'x'); Ly=diff(L,'y'); [l m n]=solve(Lx,Ly,phi-y,'x','y','lamda'); %giai he dao ham cap 1 voi: l <=> lamda , m <=> x , n <=> y. m=double(m); m(abs(imag(m))>0.0000000001)=inf;m=m-imag(m)*1i; %loai so phuc n=double(n); n(abs(imag(n))>0.0000000001)=inf;n=n-imag(n)*1i; l=double(l); l(abs(imag(l))>0.0000000001)=inf;l=l-imag(l)*1i; Lxx=diff(Lx,'x');Lyy=diff(Ly,'y');Lxy=diff(Lx,'y'); %dao ham cap 2 phix=diff(phi-y,'x');phiy=diff(phi-y,'y'); %dao ham dieu kien k=0; for i=1:length(m) %xet cuc dai, cuc tieu if m(i)==inf || n(i)==inf || l(i)==inf continue end x=m(i);y=n(i);lamda=l(i); dphi=-eval(phix)/eval(phiy); A=eval(Lxx);B=eval(Lxy);C=eval(Lyy); dL=A+B*dphi+C*dphi^2; z=eval(f); if (dL > 0 && isreal(m(i))) || ((dphi==inf || dphi==-inf) && C>0) %dao ham cap 2 > 0 => cuc tieu k=k+1; disp([' diem (' num2str(m(i)) ',' num2str(n(i)) ',' num2str(z) ') la diem cuc tieu']) text(x,y,z-1,['cuc tieu (' num2str(m(i)) ',' num2str(n(i)) ',' num2str(z) ')' ]) elseif (dL < 0 && isreal(m(i))) || ((dphi==inf || dphi==-inf) && C<0) %dao ham cap 2 < 0 => cuc dai k=k+1; disp([' diem (' num2str(m(i)) ',' num2str(n(i)) ',' num2str(z) ') la diem cuc dai']) text(x,y,z+1,['cuc dai (' num2str(m(i)) ',' num2str(n(i)) ',' num2str(z) ')' ]) end end %ve do thi if k~=0 x=linspace(min(m)-2,max(m)+2,20); y=eval(phi); [x,y]=meshgrid(x,y); z=char(f);z=strrep(z,'*','.*');z=strrep(z,'^','.^'); z=eval(f); [x y z]=khu(x,y,z); set(surf(x,y,z),'facecolor','b','edgecolor','non','facealpha',.2) x=linspace(min(m)-2,max(m)+2,20); y=eval(phi); z=eval(f); plot3(x,y,z,'color','r') else disp('ham da cho ko co cuc tri dieu kien') x=linspace(-2,2,20); y=eval(phi); [x,y]=meshgrid(x,y); z=char(f);z=strrep(z,'*','.*');z=strrep(z,'^','.^'); z=eval(f); [x y z]=khu(x,y,z); set(surf(x,y,z),'facecolor','b','edgecolor','non','facealpha',.2) hold on x=linspace(-2,2,20); y=eval(phi); z=eval(f); plot3(x,y,z,'color','r') end hold off rotate3d on end %loai cac diem bat dinh function [x y f]=khu(x,y,f) % chuong trinh loai bo cac diem khong ton tai cua ham f for i=1:length(x) for j=1:length(y) if ~isreal(f(i,j)) f(i,j)=NaN;x(i,j)=NaN;y(i,j)=NaN; end end end