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

1 次查看(过去 30 天)
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!

采纳的回答

Julia
Julia 2014-10-15
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by