Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
128 lines (120 sloc) 2.59 KB
function [s,phis,k,ds,dphi,S] = parabolas(phi,a,b,delta,epsilon)
% ref: optimization method and matlab programming - Changfeng Ma
% coder: Enneng Yang
% input:
% phi: objective function
% a,b : search region [a,b]
% delta: variable error value
% epsilon: function error value
%
% output:
% s: approximate minimum point
% phis: minimum value
% ds:¡¡The step size at the end of the iteration
% dphi: |dphi(s1)-dphi(s)|
% S: iter vector
s0 = a; maxj = 20; maxk = 30; big = 1e6; err = 1; k = 1;
S(k)=s0; cond =0; h=1; ds = 1e-5;
if(abs(s0)>1e4)
h=abs(s0)*(1e-4);
end
while(k<maxk & err>epsilon & cond~=5)
f1 = (feval(phi,s0+ds)-feval(phi,s0-ds))/(2*ds);
if(f1>0)
h = -abs(h);
end
s1 = s0+h; s2=s0+2*h; bars = s0;
phi0 = feval(phi,s0);
phi1 = feval(phi,s1);
phi2 = feval(phi,s2);
barphi = phi0;
cond = 0;
j=0;
while(j<maxj & abs(h)>delta & cond==0)
if(phi0<=phi1)
s2=s1; phi2=phi1;h=0.5*h;
s1=s0+2*h; phi1=feval(phi,s1);
else if (phi2<phi1)
s1=s2;phi1=phi2;h=2*h;
s2=s0+2*h; phi2=feval(phi,s2);
else
cond=-1;
end
end
j=j+1;
if(abs(h)>big/abs(s0)>big)
cond=5;
end
end
if(cond==5)
bars=s1; barphi=feval(phi,s1);
else
% quadratic interpolation
d = 2*(2*phi1-phi0-phi2);
if(d<0)
barh=h*(4*phi1-3*phi0-phi2)/d;
else
barh=h/3; cond=4;
end
bars = s0+barh; barphi=feval(phi,bars);
h=abs(h); h0=abs(barh);
h1 = abs(barh-h); h2 = abs(barh-2*h);
% Determine the h value for the next iteration
if(h0<h), h=h0; end
if(h1<h), h=h1; end
if(h2<h), h=h2; end
if(h==0), h=barh;end
if(h<delta),cond=1;end
if(abs(h)>big|abs(bars)>big), cond=5; end
err = abs(phi1-barphi);
so=bars;
k=k+1;
S(k) = s0;
end
if(cond ==2 & h<delta) cond=3;end
end
s=s0; phis=feval(phi,s);
ds=h; dphi=err;
end
%test£º
% f(x) = x^2 - sin(x) x in [0,1]
% [s,phis,k,ds,dphi,S]= parabolas(inline('x^2 - sin(x)'),0,1,1e-4,1e-5)
%result:
% s =
%
% 0
%
%
% phis =
%
% 0
%
%
% k =
%
% 30
%
%
% ds =
%
% 0.0233
%
%
% dphi =
%
% 6.9514e-05
%
%
% S =
%
% 1 ÖÁ 12 ÁÐ
%
% 0 0 0 0 0 0 0 0 0 0 0 0
%
% 13 ÖÁ 24 ÁÐ
%
% 0 0 0 0 0 0 0 0 0 0 0 0
%
% 25 ÖÁ 30 ÁÐ
%
% 0 0 0 0 0 0