- #1
Triathlete
- 33
- 0
I am trying to plot the magnitude of velocity and position against time but keep running into issues.
The error I am getting here is:
Subscript indices must either be real positive integers or logicals.
Error in project2 (line 46)
position(t) = w(1:3);Here is the relevant portion of my script(everything else work fine except the plot):
N = 540;
a = 1;
b = 5400;
h = (b-a)/N;
t = a;
w = [r(1);r(2);r(3);v(1);v(2);v(3)];
for t = 1:10:5400
k1 = h*rky(t,w);
k2 = h*rky(t+h/2,w+k1/2);
k3 = h*rky(t+h/2,w+k2/2);
k4 = h*rky(t+h,w+k3);
w = w + (h/6)*(k1+2*k2+2*k3+k4);
t = t+h;
position(t) = w(1:3);
velocity(t) = w(4:6);
time(t) = t;
end
subplot(2,1,1),plot(time,norm(position))
title('position');
xlabel('Time');
ylabel('Displacement');
axis([1 5400 0 10000]);
subplot(2,1,2),plot(time,norm(velocity))
title('velocity');
xlabel('Time');
ylabel('Displacement');
axis([1 5400 0 5]);
The error I am getting here is:
Subscript indices must either be real positive integers or logicals.
Error in project2 (line 46)
position(t) = w(1:3);Here is the relevant portion of my script(everything else work fine except the plot):
N = 540;
a = 1;
b = 5400;
h = (b-a)/N;
t = a;
w = [r(1);r(2);r(3);v(1);v(2);v(3)];
for t = 1:10:5400
k1 = h*rky(t,w);
k2 = h*rky(t+h/2,w+k1/2);
k3 = h*rky(t+h/2,w+k2/2);
k4 = h*rky(t+h,w+k3);
w = w + (h/6)*(k1+2*k2+2*k3+k4);
t = t+h;
position(t) = w(1:3);
velocity(t) = w(4:6);
time(t) = t;
end
subplot(2,1,1),plot(time,norm(position))
title('position');
xlabel('Time');
ylabel('Displacement');
axis([1 5400 0 10000]);
subplot(2,1,2),plot(time,norm(velocity))
title('velocity');
xlabel('Time');
ylabel('Displacement');
axis([1 5400 0 5]);