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

1 visualización (últimos 30 días)
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 81x1 matrix, and x as a 81x4 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!

Respuesta aceptada

Julia
Julia el 15 de Oct. de 2014
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.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by