Array indices must be positive integers or logical values.

it says the error is on line 13
s(t)=[0.1553567*(t^6)-2.0416*(t^5)+9.1837*(t^4)-14.829*(t^3)...
-1.3703*(t^2)+32.821*(t)-1.3155];
and I'm trying to make a loop to plot a graph for this https://screenshotscdn.firefoxusercontent.com/images/1a144e46-fb07-4a6c-9757-1788e9a3adfe.png so if you notice any errors please let me know

2 个评论

Original question, so it will be here when he tries to edit it away:
It says the error is on line 13
s(t)=[0.1553567*(t^6)-2.0416*(t^5)+9.1837*(t^4)-14.829*(t^3)...
-1.3703*(t^2)+32.821*(t)-1.3155];
and I'm trying to make a loop to plot a graph for this My Picture so if you notice any errors please let me know.
The screenshot URL is no longer valid.

请先登录,再进行评论。

回答(1 个)

What is t? You may think you are creating a function by doing this:
s(t)=[0.1553567*(t^6)-2.0416*(t^5)+9.1837*(t^4)-14.829*(t^3)...
-1.3703*(t^2)+32.821*(t)-1.3155];
But that is not how MATLAB syntax works.
In fact, MATLAB does not understand what you want. Instead, it think that s is a variable, and t is an index. is t an integer vector or array? (Clearly not, else you would not have gotten that error message.)
So, MATLAB tries to assign the element s(t), but if t is not something can be used to index, then MATLAB gets confused.

2 个评论

I mean here's my full code, i thought it would recognise t and s since i put it at the start
t=0:25; %time in seconds of the distance travelled
s=0;
for t=0:25
if t<5
s(t)=[0.1553567*(t^6)-2.0416*(t^5)+9.1837*(t^4)-14.829*(t^3)...
-1.3703*(t^2)+32.821*(t)-1.3155];
else if 5>=t<15.4
s(t)=[0.003980879*(t^5)-0.2247*(t^4)+4.8682*(t^3)-50.442*(t^2)...
+254.67*(t)-430.66];
else
s(t)=[-0.073*(t^2)+6.1802*(t)+40.423];
end
end
plot(t, s, 'b-', 'LineWidth', 2);
grid on;
fprintf('speed')
Assign to s(t+1)
I recommend that you experiment with 5>=t<15.4 as it seems unlikely to be what you want at that point.

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 Annotations 的更多信息

产品

版本

R2018b

标签

提问:

2018-11-4

Community Treasure Hunt

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

Start Hunting!

Translated by