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!