优质解答
(i)编写M文件detaf.m如下
function [f,df]=detaf(x);
f=x(1)^2+x(2)^2-x(1)*x(2)-10*x(1)-4*x(2)+60;
df(1)=2*x(1)-x(2)-10;
df(2)=2*x(2)-x(1)-4;
(ii)编写M文件tidu.m
clc
x=[2;2];
[f0,g]=detaf(x);
while norm(g)>0.001
p=-g'/norm(g);
t=1.0;f=detaf(x+t*p);
while f>f0
t=t/2;f=detaf(x+t*p);
end
x=x+t*p
[f0,g]=detaf(x)
end
在命令窗口运行tidu,回车
x =
0.9285
0.3714
f0 =
49.8848
g =
-8.5144 -4.1857
x =
1.8259
0.8126
f0 =
41.0013
g =
-7.1608