MATLAB plotting issue

  • #1
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]);
 

Answers and Replies

  • #2
BvU
Science Advisor
Homework Helper
14,480
3,752
t is your integration coordinate, not an integer index to point to a position in an array.
Depending on how position is declared you get something like

position(i) = w(1:3)

or

for j = 1 to 3
position(i,j) = w(j)
next j

but hen in matlab language
 
  • #3
DrClaude
Mentor
7,616
4,037
Matlab:
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 % don't use t here
    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); % or here
    velocity(t) = w(4:6); % or here
    time(t) = t; % or on the lhs here
end
You are using t both as the index of the for loop and as the variable containing the actual time. Also, position and velocity will need to be matrices, not vectors.
 
  • Like
Likes BvU
  • #4
33
0
Thanks for the responses!
How would I convert to matrices?
 
  • #5
kreil
Insights Author
Gold Member
668
68
Thanks for the responses!
How would I convert to matrices?

The two lines like "position(t) = w(1:3);" are curious, since you are trying to assign 3 values w(1:3) to the single spot position(t) in a vector. Why is that? Don't you just want a single value for the position at time t?

If you wanted those three values to be a row in a matrix you could do something like position(i,:)=w(1:3) using a new indexing variable i.

The current error you're receiving is because the line t = t+h makes t some decimal value that doesn't make sense as an index. Once you resolve that, you'll also get another error because you can't assign 3 values to 1 spot in a vector.

Can you do your best to update the code with the suggestions presented so far and repost it afterward? If you post a self-contained set of runnable code it is easier to debug.
 

Related Threads on MATLAB plotting issue

  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
1
Views
9K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
2
Views
4K
  • Last Post
Replies
8
Views
5K
  • Last Post
Replies
0
Views
6K
  • Last Post
Replies
2
Views
2K
  • Last Post
Replies
15
Views
1K
  • Last Post
Replies
0
Views
2K
Top