目录
算法的具体思想:
(1)选定初始点a 和步长h;
(2)计算并比较f(a)和f(a+h);有前进(1)和后退(2)两种情况
2.1 前进运算:
若f(a)>= f(a+h),则步长加倍h = 2*h,计算f(a+3h)。若f(a+h) ≤f(a+3h),令 a1=a, a2=a+3h, 停止运算;否则将步长加倍,并重复上述运算。
2.2 后退运算:
若f(a)<f(a+h),则将步长改为h=-0.25*h。计算f(a-h), f(a-h) ≥ f(a), 令 a1=a-h, a2=a+h, 停止运算;否则将步长加倍,并重复上述运算。
查找区间实现代码:
函数文件:
%%这是function函数:
function f = myfun(x)
f = x^3 - 2*x + 1 ;
success_fail.M文件:
function [a1,a2]=success_fail(x0,h0) %x0 起始点 h0 初始步长
t = x0 + h0;
h=h0;
f0 = myfun(x0);
f1 = myfun(t);
f2 = 0;
a1 = x0;
m = t;
a2 = t;
while 1
if f0 >= f1
h = 2*h;
f2 = myfun(t + h);
t = t + h;
a1 = m;
m = a2;
a2 = t;
if f1 <= f2
break;
end
f0 = f1;
f1 = f2;
elseif f0 < f1
h = -0.25*h0;
t = x0 + h;
f1 = myfun(t);
end
end
if h < 0
t = a1;
a1 = a2;
a2 = t;
end
两个例子:
这个两个例子,我用matlab都试过,正确。