Matlab programming (line search)

조회 수: 10 (최근 30일)
huang
huang 2014년 4월 11일
답변: Image Analyst 2014년 4월 11일
function alpha=linesearch(t0,n,x,y,capacity)
ALPHA=.15
BETA=4
capacity1=capacity*.9;
a=0;
b=1;
r=(5^(1/2)-1)/2;
while(abs(a-b)>.00001)
alphaleft=a+(1-r)*(b-a);
alpharight=a+r*(b-a);
zalphaleft=0;
dleft=x+alphaleft*(y-x);
dleft1=dleft./capacity1;
for(i=1:n)
zalphaleft(i)=t0(i)*dleft(i)*(1+ALPHA*(dleft1(i)^BETA));
end
zalphaleft=sum(zalphaleft);
zalpharight=0;
dright=x+alpharight*(y-x);
dright1=dright./capacity1;
for(i=1:n)
zalpharight(i)=t0(i)*dright(i)*(1+ALPHA*(dright1(i)^4));
end
zalpharight=sum(zalpharight);
if(zalphaleft<=zalpharight)
b=alpharight;
else
a=alphaleft;
end
end
alpha=(a+b)/2;
The error suggests:
Attempted to access t0(2); index out of bounds because numel(t0)=1.
Error in uesearch (line 18)
zalphaleft(i)=t0(i)*dleft(i)*(1+ALPHA*(dleft1(i)^BETA));
May I ask how to fix this problem?
  댓글 수: 2
Shashank Prasanna
Shashank Prasanna 2014년 4월 11일
Can you format your code using the Code button?
dpb
dpb 2014년 4월 11일
for(i=1:n)
zalphaleft(i)=t0(i)*...
You pass in t0 which apparently was a scalar and by name one presumes an initial value. But in the above loop you try to use it as a vector.
Only you know what it is you're actually trying to do, but that's your present coding problem.

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

답변 (1개)

Image Analyst
Image Analyst 2014년 4월 11일
Maybe just say t0 instead of t0(i). Or t0 * i. This code does not have any comments, which is a sign of poor coding (you should scold whomever gave you this), so we don't really know what the intent is. But it's probably one or the other of the solutions I gave you.

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by