# Line Search Algorithm help

30 ビュー (過去 30 日間)
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;
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
alpha = alpha(i);
return;
end
alpha = lszoom(alpha(i), alpha(i-1));
return;
end
alpha(i+1) = 2*alpha;
end
end
function [alpha] = lszoom(alphalo, alphahigh)
syms x
f = @(x)100*(x(2) - x(1)^2)^2 + (1 - 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;
alphahigh = alpham;
else
alpha = alpham;
return;
end
alphahigh = alphalo;
end
alphalo = alpham;
end
end
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)