Why I get index out of bounds error even though I debug the loop and its Ok
1 次查看(过去 30 天)
显示 更早的评论
Hi all I try to optimize three variables using fmincon. Inside the objective function there is a loop which the error message point on it. I debug the loop and its work and its reach the end and go to fmincon to complete the check after that I get the error Kindly, can any on explain why I get this error. Regards
??? Attempted to access t02(206); index out of bounds because numel(t02)=205. % t02 is a time vector (206x1)
Error in ==> ObjFun_Zdd_F at 52
ttt=t02(i+1)-t02(i);
Error in ==> @(Fkc)ObjFun_Zdd_F(Fkc)
Error in ==> E:\Program
Files\MATLAB\R2011a\toolbox\optim\optim\private\evalObjAndConstr.p>evalObjAndConstr
at 135
Error in ==> E:\Program
Files\MATLAB\R2011a\toolbox\optim\optim\sqpLineSearch.p>sqpLineSearch at 265
Error in ==> fmincon at 832
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] =
sqpLineSearch(funfcn,X,full(A),full(B),full(Aeq),full(Beq), ...
Error in ==> Fmain at 9
[Fkcopt,fval,exitflag,output] = fmincon(@(Fkc) ObjFun_Zdd_F(Fkc),Fkco,...
and this is fmincon
Fkco=[100,1000,500];
lb=[0,100,50];
ub=[3000,160000,50000];
options = optimset('Display','iter','TolFun',1e-8,'algorithm','sqp');
[Fkcopt,fval,exitflag,output] = fmincon(@(Fkc) ObjFun_Zdd_F(Fkc),Fkco,...
[],[],[],[],lb,ub,[],options);
and the objective function is:
function [val] = ObjFun_Zdd_F(Fkc)
F=Fkc(1);
kc(1)=Fkc(2);
kc(2)=Fkc(3);
load IRI_737b
zdot = calc_zdot(road_x, road_z, 10);
zt=zdot.u;
x00=[0,0,0,0];
input=@(t)lookup_u(zdot,t);
opt = odeset('RelTol', 1e-2, 'AbsTol', 1e-3);
[t02,y] = ode23(@(t,x)f(t,x,F,kc, @(t)lookup_u(zdot,t)), [0 2], x00,opt);
xx=size(t02,1);
x_frame=y';
FF=F * ones(1, xx);
xu_frame=[x_frame;FF];
val = 0;
r1 = 1e+5
r2 = 0.5;
q = 1e-5;
R = diag([r1, 0, 0, 0, q,r2]);
xu_frame_zs = zeros(6,length(t02));
for i=1:205
ddx=ff(i,xu_frame(1:4,i),F,kc);
Acc=ddx(4);
xu_frame_zs(:,i) = [xu_frame(:,i);Acc];
ttt=t02(i+1)-t02(i); % here the error pointed
val=val+ttt*xu_frame_zs(:,i)'*R*xu_frame_zs(:,i);
end
end
8 个评论
Walter Roberson
2016-7-15
At the command line, give the command
dbstop if error
and run the program. When it stops with that error message, show size(t02) and size(y) and look at xx, and between all of those figure out whether the ode routine returned 205 or 206 values.
采纳的回答
Walter Roberson
2016-7-16
Poster had hard-coded the expected number of points returned by ode23s, but ode23s decides dynamically how many points to use and was not always using the same number of points. User has changed the code to not hard-code the number of points, solving the original problem.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!