Matlab programming (line search)
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
dpb
am 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.
Antworten (1)
Image Analyst
am 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 Kommentare
Siehe auch
Kategorien
Mehr zu Performance and Memory finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!