用Matlab求多元函数的最值

2024-11-25 09:30:59
推荐回答(1个)
回答1:

这里x>=0,y>=0时比较好算一点,依据偏导数为零是的点可能为函数极值点来计算:

syms x y z;
z = (1+x)*sqrt((1-x)^2+y^2)+x^2;
dx = diff(z,'x');
dy = diff(z,'y');
%求解dx,dy的偏导数为零的x和y
X = solve('2*x + ((x - 1)^2 + y^2)^(1/2) + ((2*x - 2)*(x + 1))/(2*((x - 1)^2 + y^2)^(1/2))=0','x');
Y = solve('(y*(x + 1))/((x - 1)^2 + y^2)^(1/2)=0','y');
%得到X=y^2/4,Y=0,当偏导数为零时函数取得极值,又有已知条件x,y的范围得到x,y都为零是有极值
x0=0;y0=0;
Z = (1+x0)*sqrt((1-x0)^2+y0^2)+x0^2;
fprintf('极小值为:%i\n',Z);