用进退法确定函数f(x)=x^2-6x+9的一优化搜索区间[a,b].设初始点x=0,初始步长h=0.用matlab程序,急求!!
1个回答
展开全部
首先你的建立三个M函数文件,分别建立文件夹:
(1)%typbound.m;
function [lowbound,upbound]=typbound(x0,step0,startopint,searchdirection)
step=step0;
f0=tryobjfun(x0,startopint,searchdirection);
x1=x0+step0;
f1=tryobjfun(x1,startopint,searchdirection);
if f1<=f0
while true
step=2*step;
x2=x1+step;
f2=tryobjfun(x2,startopint,searchdirection);
if f1<=f2
lowbound=x0;
upbound=x2;
break;
else
x0=x1;
x1=x2;
f0=f1;
f1=f2;
end
end
else
while true
step=2*step;
x2=x0-step;
f2=tryobjfun(x2,startopint,searchdirection);
if f0<=f2
lowbound=x2;
upbound=x1;
break;
else
x1=x0;
x0=x2;
f1=f0;
f0=f2;
end
end
end
%(2)tryobjfun.m
function f=tryobjfun(a,startopint,searchdirection)
f=objfun(startopint+a.*searchdirection);
%(3)确定函数,也就是你要确定搜索区间的目标函数,你也可以改,这仅是一个例子
function f=objfun(x)
f=x(1)^3+x(2)^2-10*x(1)*x(2)+1;
在命令窗口调用建立的函数
%%0是初始探测点,0.01是初始探测步长,[0,0]是初始搜索点,[1,1]是方向
>>[low,up]=typbound(0,0.01,[0,0],[1,1])
%%运行结果为
low =
2.550000000000000
up =
10.230000000000000
(1)%typbound.m;
function [lowbound,upbound]=typbound(x0,step0,startopint,searchdirection)
step=step0;
f0=tryobjfun(x0,startopint,searchdirection);
x1=x0+step0;
f1=tryobjfun(x1,startopint,searchdirection);
if f1<=f0
while true
step=2*step;
x2=x1+step;
f2=tryobjfun(x2,startopint,searchdirection);
if f1<=f2
lowbound=x0;
upbound=x2;
break;
else
x0=x1;
x1=x2;
f0=f1;
f1=f2;
end
end
else
while true
step=2*step;
x2=x0-step;
f2=tryobjfun(x2,startopint,searchdirection);
if f0<=f2
lowbound=x2;
upbound=x1;
break;
else
x1=x0;
x0=x2;
f1=f0;
f0=f2;
end
end
end
%(2)tryobjfun.m
function f=tryobjfun(a,startopint,searchdirection)
f=objfun(startopint+a.*searchdirection);
%(3)确定函数,也就是你要确定搜索区间的目标函数,你也可以改,这仅是一个例子
function f=objfun(x)
f=x(1)^3+x(2)^2-10*x(1)*x(2)+1;
在命令窗口调用建立的函数
%%0是初始探测点,0.01是初始探测步长,[0,0]是初始搜索点,[1,1]是方向
>>[low,up]=typbound(0,0.01,[0,0],[1,1])
%%运行结果为
low =
2.550000000000000
up =
10.230000000000000
参考资料: 吴祈宗,郑志勇,邓伟.运筹学与最优化matlab编程[M].北京:机械工业出版社,2009.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询