why do i receive this error?

3 Ansichten (letzte 30 Tage)
shamma aljaberi
shamma aljaberi am 19 Jan. 2023
Kommentiert: shamma aljaberi am 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.

Akzeptierte Antwort

Dyuman Joshi
Dyuman Joshi am 19 Jan. 2023
Bearbeitet: Dyuman Joshi am 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')

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by