优化设计的一个问题,两种方法结果为什么不一样 。
Show older comments
看书本遇到一个问题:最小化函数 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
More Answers (0)
Categories
Find more on 选择求解器 in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!