why do i receive this error?

3 vues (au cours des 30 derniers jours)
shamma aljaberi
shamma aljaberi le 19 Jan 2023
Commenté : shamma aljaberi le 20 Jan 2023
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.

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 19 Jan 2023
Modifié(e) : Dyuman Joshi le 20 Jan 2023
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 commentaire
shamma aljaberi
shamma aljaberi le 20 Jan 2023
Thank you! it worked

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Help Center et File Exchange

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by