function test
step=0.1; %步长
n=0;
x=[0 0 0]; %初始点
interval=[-10:0.01:20]; %图像区间
plot(interval,f(interval))
hold on
while f(x(1))<=f(x(2))|f(x(2))>=f(x(3))
x(1)=x(2)
x(2)=x(3)
x(3)= x(3)+step*2^n
n=n+1;
if x(3)>10^5
x=[0 0 0];
step=-0.1;
n=0
end
end
plot(x,f(x),'o');
a=x(1);b=x(3);
x1=0.382*(b-a)+a
x2=0.618*(b-a)+a
while (b-a)/b>0.0001
if f(x1)<f(x2)
b=x2;
x2=x1;
x1=0.382*(b-a)+a;
else
a=x1;
x1=x2;
x2=0.681*(b-a)+a;
end
end
xo=(a+b)/2
plot([x1,x2],f([x1,x2]),'r*')
end
function a=f(x)
a=x.^2-10*x+36; % 代求方程
end
【MATLAB】进退法求取单谷区间并黄金分割法求极值
最新推荐文章于 2023-02-12 11:54:09 发布