- %不精确一维搜索法
- function result=Usearch2(f,x1,x2,df,x0,p)
- mu=0.4;sgma=0.8;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<sgma*gk1'*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)});gk2=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;
- end
- end;
- result=[arf,m_count,s_count];
复制代码 |