why do i receive this error?

2 ビュー (過去 30 日間)
shamma aljaberi
shamma aljaberi 2023 年 1 月 19 日
コメント済み: shamma aljaberi 2023 年 1 月 20 日
clc, clear
t=0:1:20
S(t)=2*t.^2
V(t)=diff(S(t));
a(t)=diff(V(t));
subplot(1,3,1)
plot(t,S(t))
title('Position vs. Time')
xlabel('Time in seconds')
ylabel('Position in meters')
subplot(1,3,2)
plot(t,V(t))
title('Velocity vs. Time')
xlabel('Time in seconds')
ylabel('Velocity in m/s')
subplot(1,3,3)
plot(t,a(t))
title('Acceleration vs. Time')
xlabel('Time in seconds')
ylabel('Acceleration in m/s^2')
This is my code but it shows:
Array indices must be positive integers or logical values.
Error in (line 4)
S(t)=2*t.^2
i think its something related to the time array.

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 1 月 19 日
編集済み: Dyuman Joshi 2023 年 1 月 20 日
The error occurs because 0 can not be an index in MATLAB (Indexing starts from 1) and you tried to initialize the variable S (V and T as well) with 0
What you are trying to do is quite different than the code you wrote.
This should give what you are looking for -
syms S(t)
S(t)=2*t.^2;
V(t)=diff(S(t));
a(t)=diff(V(t));
figure
subplot(1,3,1)
fplot(t,S(t),[0 20])
title('Position vs. Time')
xlabel('Time in seconds')
ylabel('Position in meters')
subplot(1,3,2)
fplot(t,V(t),[0 20])
title('Velocity vs. Time')
xlabel('Time in seconds')
ylabel('Velocity in m/s')
subplot(1,3,3)
fplot(t,a(t),[0 20])
title('Acceleration vs. Time')
xlabel('Time in seconds')
ylabel('Acceleration in m/s^2')
  1 件のコメント
shamma aljaberi
shamma aljaberi 2023 年 1 月 20 日
Thank you! it worked

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

その他の回答 (0 件)

カテゴリ

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

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by