|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
二元函数黄金分割法的直线搜索
function [a,b,z]=lsliu(f,z0,h)
syms x1 x2 y1 y2 z1 z2
y0=subs(f,[x1,x2],z0);
%n=0
z2=z0+h;
y2=subs(f,[x1,x2],z2);
if(y2>=y0)
z1=z2;
y1=y2;
h=-h;
else
z1=z0;
y1=y0;
z0=z2;
y0=y2;
h=2*h;
end
z2=z0+h;
y2=subs(f,[x1,x2],z2);
while(y2<=y0)
z1=z0;
y1=y0;
z0=z2;
y0=y2;
h=2*h;
z2=z0+h;
y2=subs(f,[x1,x2],z2);
end
z1=z0;
a=min(z1,z2);
b=max(z1,z2);
eps=0.05;
z1=a+0.382*(b-a);
y1=subs(f,[x1,x2],z1);
z2=a+0.618*(b-a);
y2=subs(f,[x1,x2],z2);
while (abs(b-a)>eps);
if (y1>=y2)
a=z1;
b=b;
z1=a+0.382*(b-a);
y1=subs(f,[x1,x2],z1);
z2=a+0.618*(b-a);
y2=subs(f,[x1,x2],z2);
elseif y2>y1
a=a;
b=z2;
x1=a+0.382*(b-a);
y1=subs(f,[x1,x2],z1);
z2=a+0.618*(b-a);
y2=subs(f,[x1,x2],z2);
end
end
z=(z1+z2)/2;
运行后 :
??? Function 'ge' is not defined for values of class 'sym'.
Error in ==> lsliu at 38
if (y1>=y2)
为什么不能比较
[ 本帖最后由 xinyuxf 于 2007-7-22 16:10 编辑 ] |
|