Why does the subscript indices error mean in my code?
Show older comments
Hi, I'm a new user. My apologies if the answer is devastatingly simple, but I can't seem to understand why I keep getting this error 'Subscript indices must either be real positive integers or logicals.
Error in CWIIIQ1 (line 13)
in my code. The total code is as follows:
t = [0, 10, 15, 20, 32, 59, 62, 125]; %denotes values of time
v = [0, 56.4, 97.23, 136.25, 226.16, 403.86, 440.44, 1265.23]; %denotes velocity measurements in m/s
format short e; % changes output values to scientific notation with 5 significant figures
p = polyfit(t,v,4); %plots coefficients of a 4th degree polynomial that approximates a function to the data set, choose 4th degree as it results in most accurate fit
%equation polyfit gives is v(t) = (1.14*10e-05)*(x^4)-(1.94e-03)*(x^3)+
%0.10*x^2 + 5.42*x -1.54 with t replacing x in reality
pvx = polyval(p,t); %creates time values for approximated quartic function in array form
v(t) = (1.14*10^-5)*(pvx.^4)-(1.94*10^-3)*(pvx.^3) + 0.10*pvx.^2 + 5.42*pvx -1.54; %this is the velocity equation polyfit calculated
s(t) = int(v(t)); %this is the position equation of the rocket,which is equal to distance travelled as it always is in positive direction
a(t) = diff(v(t),x); %this is the acceleration of the rocket
figure
subplot(2,2,1) %creates first subplot
plot(x, v(t))
title('Velocity of Rocket versus Time')
subplot(2,2,2)
plot(x, s(t))
title('Position of Rocket versus Time')
subplot(2,2,3)
plot(x, a(t))
title('Acceleration of Rocket versus Time')
Thanks a lot for the help.
2 Comments
Roger Stafford
on 6 Dec 2017
The "devastatingly simple answer" is that all matlab indices must be real positive integers, and yours aren't. You have defined both t and v as vectors, so writing v(t) violates that rule, because t is being interpreted, unavoidably, as a vector of indices to v. You will have to express your computations in some other way.
Walter Roberson
on 6 Dec 2017
If you could assign to v(t) with t being non-integer, you would have the problem on the next line with int(v(t)) : int() is for symbolic integration of a symbolic expression or symbolic function but you are trying to int() a numeric vector.
Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!