# Line Search Algorithm help

조회 수: 248(최근 30일)
Sarah Johnson 2020년 2월 20일
댓글: Mali Norouzi 2021년 6월 23일
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)
##### 댓글 수: 1표시숨기기 없음
Mali Norouzi 2021년 6월 23일
hi sarah, did you find the answer? i had a problem such this\:(

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by