如何用matlab求多元函数的极值

求 在 0<x<pi/2, 0<y< pi/2内的极大值.
2024-11-18 22:40:00
推荐回答(2个)
回答(1):

当X=pi/3 y=pi/3 z有极大值 3/8*3^(1/2) %定义二元函数
clc;clear;
syms x y;
z=sin(x)*sin(y)*sin(x+y);
ezsurf(x,y,z,[0,pi/2,0,pi/2]);
%求解驻点
dzx=diff(z,x)
dzy=diff(z,y)
[xx,yy]=solve('cos(x)*sin(y)*sin(x+y)+sin(x)*sin(y)*cos(x+y)=0',...
'sin(x)*cos(y)*sin(x+y)+sin(x)*sin(y)*cos(x+y)=0','x','y')
%对于定义域内的驻点求解二阶偏导函数
A=diff(z,x,2);
B=diff(diff(z,x),y);
C=diff(z,y,2);
D=A*C-B^2;
%判别极大值点
D1=subs(subs(D,'x',xx(1)),'y',yy(1))
A1=subs(subs(A,'x',xx(1)),'y',yy(1))
D2=subs(subs(D,'x',xx(2)),'y',yy(2))
A2=subs(subs(A,'x',xx(2)),'y',yy(2))
%极大值点和极大值
[xx(2) yy(2)]
JDZ=subs(subs(z,'x',xx(2)),'y',yy(2)) 程序运行结果ans =

[ 1/3*pi, 1/3*pi]
JDZ =

3/8*3^(1/2)

回答(2):

on来求解。写成向量形式,约束A*x<=b,初始值x0附近可以搜出出局部极小值。输入:
z=@(x)7.2*sqrt(25+(15-x(1))^2)+(7.2+(107/(20-x(1))))*sqrt((8-x(2))^2+(20-x(1))^2)+7.2*x(2);
A=[1,0;0,1];
b=[15;8];
x0=[1;1];
[x,feval]=fmincon(z,x0,A,b)
得到局部极小值的坐标和函数值:
x
=
15.0000
6.6994
feval
=
231.9944