Line Search Algorithm help

81 visualizaciones (últimos 30 días)
Sarah Johnson
Sarah Johnson el 20 de Feb. de 2020
Editada: Daniele Mori el 21 de Abr. de 2023
This part is just the background to the algorithms I am working on:
Here is the code I currently have but I'm not sure what to do to get anything to work:
function [alpha] = linesearch(nsteps)
syms x
f = @(x)100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
grad = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1)); 200*(x(2)-x(1)^2)];
gradTwo = [800*x(1)+2, -400*x(1); -400*x(2), 200];
alpha(0) = 0;
alpha(1) = 1;
c1 = 10^-4;
c2 = 0.9;
for i = 1:nsteps
if (f(alpha(i)) > f(0)+c1*grad(0)||(f(alpha(i)) > f(alpha(i-1))))
alpha = lszoom(alpha(i-1), alpha(i));
return;
end
if abs(grad(alpha(i))) <= abs(c2*grad(0))
alpha = alpha(i);
return;
end
if grad(alpha(i)) >= 0
alpha = lszoom(alpha(i), alpha(i-1));
return;
end
alpha(i+1) = 2*alpha;
end
Error('step size alpha not found within 10 iterations')
end
function [alpha] = lszoom(alphalo, alphahigh)
syms x
f = @(x)100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
grad = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1)); 200*(x(2)-x(1)^2)];
gradTwo = [800*x(1)+2, -400*x(1); -400*x(2), 200];
alpha(0) = 0;
alpha(1) = 1;
c1 = 10^-4;
c2 = 0.9;
nsteps = 50;
for i = 1:nsteps
alpham = (alphalo + alphahigh) / 2;
if (f(alpham) > alpha(0)+c1*alpham*grad(0)||f(alpham)>f(alphalo))
alphahigh = alpham;
else
if abs(grad(alpham)) <= c2*abs(grad(0))
alpha = alpham;
return;
end
if grad(alpham)*(alphahigh-alphalo) >= 0
alphahigh = alphalo;
end
alphalo = alpham;
end
end
Error('step length alpha not found within 50 iterations')
end
Both algorithms give me the error:
"Index exceeds the number of array elements (1)
Error in sym/subsref (line 900)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in lszoom (line 4)
grad = [-400*(x(2)-x(1)^2)*x(1)-2*(1-x(1)); 200*(x(2)-x(1)^2)];"
  1 comentario
Mali Norouzi
Mali Norouzi el 23 de Jun. de 2021
hi sarah, did you find the answer? i had a problem such this\:(

Iniciar sesión para comentar.

Respuestas (2)

ahmed abrous
ahmed abrous el 16 de Mayo de 2022
Hello
can you put the reference of the book please.
Regards.

Daniele Mori
Daniele Mori el 21 de Abr. de 2023
Editada: Daniele Mori el 21 de Abr. de 2023
Hi Sarah,
I modified your code and now it seems to work.
If you have any questions, please tell me.
Best reguards,
Daniele
syms x y
fun = 100*(y - x^2)^2 + (1 - x)^2;
d = gradient(fun);
f = matlabFunction(fun);
grad = matlabFunction(d);
x_0 = 5;
y_0 = 5;
in_point = [x_0,y_0];
s = -grad(x_0,y_0);
phi = @(alpha)f(in_point(1) + alpha*s(1),in_point(2) + alpha*s(2));
d_phi = @(alpha)grad(in_point(1) + alpha*s(1),in_point(2) + alpha*s(2))'*s;
alpha_0 = 0;
alpha_max = 1;
alpha(1) = (alpha_0 + alpha_max)/2;
c1 = 10^-4;
c2 = 0.1;
i = 1;
while 1
if (phi(alpha(i) > phi(0)+c1*alpha(i)*d_phi(0)) || (phi(alpha(i)) > phi(alpha_0)) && i>1)
[alpha_star,n] = lszoom(alpha_0,alpha(i),phi,d_phi);
break;
end
if abs(d_phi(alpha(i))) <= abs(c2*d_phi(0))
alpha_star = alpha(i);
break;
end
if d_phi(alpha(i)) >= 0
[alpha_star,n] = lszoom(alpha(i),alpha_0,phi,d_phi);
break;
end
alpha_0 = alpha(i);
alpha(i+1) = (alpha_0 + alpha_max)/2;
i = i+1;
end
function [alpha_star,n] = lszoom(alphalo,alphahigh,phi,d_phi)
c1 = 10^-4;
c2 = 0.1;
n=1;
while 1
alpham = (alphalo + alphahigh) / 2;
if (phi(alpham) > phi(0)+c1*alpham*d_phi(0)||phi(alpham)>phi(alphalo))
alphahigh = alpham;
else
if abs(d_phi(alpham)) <= -c2*d_phi(0)
alpha_star = alpham;
return;
end
if d_phi(alpham)*(alphahigh-alphalo) >= 0
alphahigh = alphalo;
end
alphalo = alpham;
end
n = n+1;
end
end

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by