MATLAB: Matlab trying to access index zero of a matrix, even though the boundaries are specified!

access index 0out of bounds

Hi! I have this Matlab problem that's driving me crazy! I'm trying to get values from a matrix and use these in other functions, but Matlab is giving me errors that doesn't make any sense. Here's the code: (R,W,T and v0 are constants declared in the beginning of the code. [t,x] returns t as a 81×1 matrix, and x as a 81×4 matrix).
[t,x]=ode45(@func2,[0 T],[0;v0;0;0]);
r=@(i)R-x(i,1);
fi=@(i)W*t(i)+(x(i,2));
er=@(alpha)[cos(alpha);sin(alpha)];
for i=1:length(t)
RV(i,:)=R*er(W*t(i));
RD(i,:)=r(t(i))*er(fi(i));
end
for i=1:length(t)
RB(i)=abs(RV(i,:)-RD(i,:));
end
plot(RB)
Here's the problem: When I run this i get the error:
"Attempted to access x(0,1); index must be a positive integer or logical.
Error in @(i)R-x(i,1)"
Even though it is specified in the for-loops that the value of i should start with 1. Declaring i as a variable with the value 1 doesn't help, neither does changing the name from i to something else in the functions before the for-loops. I don't understand why it is trying to access index 0!
Would greatly appreciate any help i can get!

Best Answer

  • Hi,
    the error occurs for the second line:
    r=@(i)R-x(i,1);
    You use i as the argument of that function. So i can have the value 0.