📄 新建文本文档.txt
字号:
Fpush.m文件
function [x1,x2]=Fpush(t0,h)
%进退法,调用函数为,F618Ret,t0为初始值,h为步长
a=t0;
b=t0+h;
while 1
if F618Ret(a)<F618Ret(b)
h=0-h;
a=a+h;
b=a-h;
if F618Ret(b)<F618Ret(b-h) & F618(b)<F618Ret(a)
break;
end
else
a=b;
b=a+h;
if F618Ret(a)<F618Ret(b) & F618Ret(a)<F618Ret(a-h)
break;
end
end
end
x1=a;
x2=b;
%============================================
%测试数据
% >> [x1,x2]=Fpush(0,0.8);
% >> x1
%
% x1 =
%
% 4.8000
%
% >> x2
%
% x2 =
%
% 5.6000
%
% >>
F618.m文件
function [t,min]=F618(precision,t0,h)
%初始区间为a,b,相对精度precision 即为原区间的多少分之,t0为初始值,h为步长
%用进退法得到高低高区间,在用0.618法求最值
%precision=0.001;
[a,b]=Fpush(t0,h);
An=a;
Bn=b;
n=1;
while 0.618^(n-1)>=precision
A=0.618.*(An-Bn)+Bn;
B=0.618.*(Bn-An)+An;
if A<B
if F618Ret(A)<F618Ret(B)
Bn=B;
else
An=A;
end
else
if F618Ret(A)<F618Ret(B)
An=A;
else
Bn=B;
end
end
n=n+1;
end
t=An;
min=F618Ret(An);
%=======================================
%测试过程
% >> [t,min]=F618(0.00001,0,0.8);
% >> t
%
% t =
%
% 4.9999
%
% >> min
%
% min =
%
% 11.0000
%========================================
F618Ret.m文件
function y=F618Ret(t)
%函数为f(x)=t^2-10*t+36 严格凸函数
y=t.^2-10*t+36;
新建以上三个m文件,保存到工作目录下就可以运行
>> clear all
>> [t,min]=F618(0.00001,0,0.8)
t =
5.0000
min =
11.0000
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -