matlab

The gold function is as follows:


function [ as,x,f,k ] = gold( func,x0,d0,amin,amax,t )

%YIWEIXIANXING A summary about this function is shown here
%function call format [optimal step size a, next x, corresponding function value f, iteration number k]=gold(@func, x0,d0,amin,amax,t )
%func objective function
%x0 initial value
%d0 initial direction
%amin step size lower bound
%amax step size upper bound%terror
range
% Show detailed description here
rate=2/(1+sqrt(5));
k=0 ;
amink=amin;
amaxk=amax;
l=rate*(amaxk-amink);
alk=amaxk-l;
ark=amink+l;
xlk=x0+alk*d0;
xrk=x0+ark*d0;
flk=func (xlk);
frk=func(xrk);
while abs(amaxk-amink)>t
    k=k+1;
    if (flk<=frk)
        amaxk=ark;
        l=rate*(amaxk-amink);
        ark=alk ; 
        frk=flk;
        alk=amaxk-l;
        xlk=x0+alk*d0;
        flk=func(xlk);
    else
        amink=alk;
        l=rate*(amaxk-amink);
        alk=ark;
        flk=frk;
        ark=amink+l;
        xrk=x0+ark*d0;
        frk=func(xrk);
    end
end


as=0.5*(amaxk+amink);
x=x0+as*d0;
f=func(x);
        

end


when entering

>> gold( f_test2,[1,1],[1,1],0,2,1e-15 )
error Using f_test2 (line 4)
insufficient number of input arguments.


when entering

[as x f k]=gold(@booth,[1,1],[1,1],0,2,0.000001)


as =


    1.0000



The correct input should be

>> [as x f k]=gold(@f_test2,[2,2],[-1,-1],0,2,1e-6)


as =


    2.0000




x =


   1.0e-06 *


    0.3322    0.3322




f =


   -1.0000




k =


    31



The output of the function should also be assigned a value, which is very strange. I thought it would output ans directly, but it didn't work. Just need the complete output.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325423781&siteId=291194637