Array indices must be positive integers or logical values.

2 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Les Beckham
Les Beckham am 29 Dez. 2019
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.

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by