Array indices must be positive integers or logical values.

2 ビュー (過去 30 日間)
Madara Hettigama
Madara Hettigama 2019 年 12 月 29 日
回答済み: Les Beckham 2019 年 12 月 29 日
I have defined values of t as: t= 0:142.27
and then the function: h(t)= (30^(5/2)-(5*sqrt(2)*9.8/2)*t).^(2/5) to get values for h(t)
but it comes up with the notification "Array indices must be positive integers or logical values", Not sure why this is happening

採用された回答

Les Beckham
Les Beckham 2019 年 12 月 29 日
The error text pretty much explains why you are getting an error.
"Array indices must be positive integers or logical values"
Matlab arrays are indexed with array indices that start with 1 for the first index in the array.
You are trying to index using t= 0:142.27 which starts at zero (which is not a valid array index in Matlab). Note also that, since you have not provided a step size for constructing this vector, the last element of the vector will be 142 (not 142.27) because Matlab assumes a step size of one if you don't tell it otherwise.
You probably need to define your t vector with a step size (for example t = [0:0.01:142.27]) and then, using the fact that Matlab operates on vectors as well as scalars, you can get your answer for the whole vector at once with this:
h = (30^(5/2)-(5*sqrt(2)*9.8/2)*t).^(2/5) ;
Note that there is not a (t) on the left hand side. You correctly had the .^ instead of ^ to get element-wise operations but the left hand side can accept the entire vector result without the index.
I hope this helps.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by