Matlab programming (line search)
2 views (last 30 days)
Show older comments
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 Comments
dpb
on 11 Apr 2014
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.
Answers (1)
Image Analyst
on 11 Apr 2014
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.
0 Comments
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!