最优化方法实验报告2
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
最优化方法实验报告
实验一:用进退法求函数 的一个形如 的初始区间。
程序如下:
function [minx,maxx] = minJT(f,x0,h0,eps)
format long;
if nargin == 3
eps = 1.0e-6;
end
x1 = x0;
k = 0;
()22+-=x x x f []b ,0
h = h0;
while 1
x4 = x1 + h;
k = k+1;
f4 = subs(f, findsym(f),x4); f1 = subs(f, findsym(f),x1); if f4 < f1
x2 = x1;
x1 = x4;
f2 = f1;
f1 = f4;
h = 2*h;
else
if k==1
h = -h;
x2 = x4;
f2 = f4;
else
x3 = x2;
x2 = x1;
x1 = x4;
break;
end
end
end
minx = min(x1,x3);
maxx = x1+x3 - minx;
format short;
窗口中输入:
syms x;
f= x*x-x+2;
[minx,maxx] = minJT(f,0,0.01,eps) minx =
0.3100
maxx =
1.2700
实验二:用黄金分割法求函数在区间[]3,1-上的极小
()2
2+
-
=x
x
x
f
点。
程序为:
function [x,minf] = minHJ(f,a,b,eps) format long;
if nargin == 3
eps = 1.0e-6;
end
l = a + 0.382*(b-a);
u = a + 0.618*(b-a);
k=1;
tol = b-a;
while tol>eps && k<100000
fl = subs(f , findsym(f), l);
fu = subs(f , findsym(f), u);
if fl > fu
a = l;
l = u;
u = a + 0.618*(b - a);
else
b = u;
u = l;
l = a + 0.382*(b-a);
end
k = k+1;
tol = abs(b - a);
end
if k == 100000
disp('找不到最小值!');
x = NaN;
minf = NaN;
return;
end
x = (a+b)/2;
minf = subs(f, findsym(f),x);
format short;
在窗口中输入:
>> syms x;
>> f=x^2+x-2;
>> [x,fx]=minHJ(f,-1,3)
x =
-0.5115
fx =
-2.249
实验三:用wolf 准则求函数()()()212
2121100x x x x f -+-= 在点()T k x 0,0=沿方向()T
k p 0,1=的近似步长。 程序为:调用到的Usearch1程序:
function result=Usearch1(f,x1,x2,df,x0,p)
mu=0.001;sgma=0.99;a=0;b=inf;arf=1;m_count=0;s_count=0;
pk=p;
x3=x0;
x4=x3+arf*pk;m_count=m_count+2;s_count=s_count+2;
f1=subs(f,{x1,x2},{x3(1,1),x3(2,1)});m_count=m_count+4;s_count=s_count+3; f2=subs(f,{x1,x2},{x4(1,1),x4(2,1)});m_count=m_count+4;s_count=s_count+3; gk1=subs(df,{x1,x2},{x3(1,1),x3(2,1)});m_count=m_count+7;s_count=s_count+4; gk2=subs(df,{x1,x2},{x4(1,1),x4(2,1)});m_count=m_count+7;s_count=s_count+4; while (f1-f2<=-mu*arf*gk1'*pk)
b=arf;arf=(a+arf)/2;x4=x3+arf*pk;f2=subs(f,{x1,x2},{x4(1,1),x4(2,1)});gk2=subs(df,{x1,x2},{x4(1,1),x4(2,1)});
m_count=m_count+19;s_count=s_count+12;
end;
m_count=m_count+5;s_count=s_count+2;
while (1>0)
if(gk2'*pk a=arf;a=min(2*arf,(a+b)/2);x4=x3+arf*pk;f2=subs(f,{x1,x2},{x4(1,1),x4(2,1)});gk 2=subs(df,{x1,x2},{x4(1,1),x4(2,1)}); m_count=m_count+21;s_count=s_count+12; while (f1-f2<=-mu*arf*gk1'*pk) b=arf;arf=(a+arf)/2;x4=x3+arf*pk;f2=subs(f,{x1,x2},{x4(1,1),x4(2,1)});gk2=subs(df,{x1,x2},{x4(1,1),x4(2,1)}); m_count=m_count+19;s_count=s_count+12; end; m_count=m_count+5;s_count=s_count+2; else break;