优化设计的一个问题,两种方法结果为什么不一样 。

看书本遇到一个问题:最小化函数 f(x)=3*x1^2+2*x1*x2+x2^2书上给两种方法:1)function f=myfun(x)
f=3*x(1)^2+2*x(1)*x(2)+x(2)^2;
然后调用fminunc函数求【1,1】附近的最小值
x0=[1,1];
[x,fval]=fminunc(@myfun,x0)
返回 x =1.0e-06 *
0.2541 -0.2029
fval =
1.3173e-13
2)用梯度g使函数最小化
function [f,g]=myfun1(x)
f=3*x(1)^2+2*x(1)*x(2)+x(2)^2;
if nargout>1
g(1)=6*x(1)+2*x(2);
g(2)=2*x(1)+2*x(2);
end
然后再command window里输入
options=optimset('gradobj','on');
x0=[1,1];
[x,fval]=fminunc(@myfun1,x0,options)
得到 x =1.0e-15 *
0.3331 -0.4441
fval =
2.3419e-31
为什么两种方法的结果不一样呢,求大神解释

 Accepted Answer

filoko
filoko on 22 Nov 2022

0 votes

第二种方法指定了梯度,迭代算法会按照你提供的梯度信息去迭代计算
第一种方法没有指定梯度信息,迭代算法按照默认路径迭代计算

More Answers (0)

Tags

Asked:

on 22 Nov 2022

Answered:

on 22 Nov 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!