matlab外推法确定函数f(x)=3x∧2-8x+9的一维优化程序

2025-06-27 14:29:37
推荐回答(1个)
回答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