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

1 回表示 (過去 30 日間)
Philip
Philip 2014 年 10 月 15 日
コメント済み: Philip 2014 年 10 月 15 日
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.
  2 件のコメント
Iain
Iain 2014 年 10 月 15 日
r(t(i)) - Thats probably what's causing the problem. - t(1) ought to be 0...
Philip
Philip 2014 年 10 月 15 日
Oh! I get it! Thank you so much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by