필터 지우기
필터 지우기

why do i receive this error?

조회 수: 1 (최근 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')

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by