Matlab programming (line search)
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
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
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!