MATLAB Answers

Line Search Algorithm help

30 ビュー (過去 30 日間)
Sarah Johnson
Sarah Johnson 2020 年 2 月 20 日
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)];"

  0 件のコメント

サインイン to comment.

回答 (0 件)

サインイン してこの質問に回答します。

タグ


Translated by