多元函数求极值点相对比较麻烦一些,我在一个教程见过,转过来你参考一下吧
求
的极小值点。它即是著名的 Rosenbrock's "Banana" 测试函数。该测试函数有一片浅谷,许多算法难以越过此谷。
(1)从三维等位线图初步观察测试函数
x=-3:0.1:3;y=-2:0.1:4;
[X,Y]=meshgrid(x,y);
F=100*(Y-X.^2).^2+(1-X).^2;
contour3(X,Y,F,300),
xlabel('x'),ylabel('y'),axis([-3,3,-2,4,0,inf]),view([161,22])
hold on,plot3(1,1,0,'.r','MarkerSize',20),hold off
(2)本例采用内联函数表示测试函数如下
ff=inline('100*(x(2)-x(1)^2)^2+(1-x(1))^2','x');
(3)用单纯形法求极小值点
x0=[-1.2,1];[sx,sfval,sexit,soutput]=fminsearch(ff,x0)
Optimization terminated successfully:
the current x satisfies the termination criteria using OPTIONS.TolX of 1.000000e-004
and F(X) satisfies the convergence criteria using OPTIONS.TolFun of 1.000000e-004
sx =
1.0000 1.0000
sfval =
8.1777e-010
sexit =
1
soutput =
iterations: 85
funcCount: 159
algorithm: 'Nelder-Mead simplex direct search'
(4)用拟牛顿法求极小值点
[ux,sfval,uexit,uoutput,grid,hess]=fminunc(ff,x0)
Warning: Gradient must be provided for trust-region method;
using line-search method instead.
> In D:\MAT53\toolbox\optim\fminunc.m at line 202
Optimization terminated successfully:
Current search direction is a descent direction, and magnitude of
directional derivative in search direction less than 2*options.TolFun
ux =
1.0000 1.0000
sfval =
1.9118e-011
uexit =
1
uoutput =
iterations: 26
funcCount: 162
stepsize: 1.2992
firstorderopt: 5.0023e-004
algorithm: 'medium-scale: Quasi-Newton line search'
grid =
1.0e-003 *
-0.5002
-0.1888
hess =
820.4031 -409.5497
-409.5497 204.7720
[ 本帖最后由 happy 于 2006-10-20 10:30 编辑 ] |