Borrar filtros
Borrar filtros

why do i receive this error?

1 visualización (últimos 30 días)
shamma aljaberi
shamma aljaberi el 19 de En. de 2023
Comentada: shamma aljaberi el 20 de En. de 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.

Respuesta aceptada

Dyuman Joshi
Dyuman Joshi el 19 de En. de 2023
Editada: Dyuman Joshi el 20 de En. de 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')

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by