Resolving MATLAB Plotting Issue with Velocity & Position

  • Context: MATLAB 
  • Thread starter Thread starter Triathlete
  • Start date Start date
  • Tags Tags
    Matlab Plotting
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB plotting issue related to the integration of velocity and position against time. Participants are addressing errors encountered in the script, particularly focusing on indexing and data structure issues within the context of numerical integration.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant reports an error message indicating that "Subscript indices must either be real positive integers or logicals," suggesting issues with how indices are being used in the code.
  • Another participant points out that the variable 't' is being used both as the integration coordinate and as an index, which could lead to confusion and errors in indexing.
  • There are suggestions that the 'position' and 'velocity' variables need to be matrices rather than vectors to accommodate multiple values being assigned at once.
  • A participant questions the logic behind assigning three values to a single index in the 'position' vector, proposing that a matrix structure might be more appropriate for storing these values.
  • There is a request for clarification on how to convert the current vector assignments into matrix assignments, indicating a need for further guidance on structuring the data correctly.

Areas of Agreement / Disagreement

Participants express differing views on the appropriate use of indexing and data structures in MATLAB. There is no consensus on the best way to resolve the issues presented, and multiple competing approaches are suggested.

Contextual Notes

Participants note that the current implementation leads to decimal values for 't' due to the incrementing process, which complicates its use as an index. Additionally, the assignment of multiple values to a single index in a vector is highlighted as a source of error.

Who May Find This Useful

Individuals working with MATLAB for numerical integration and plotting, particularly those encountering similar indexing and data structure issues in their scripts.

Triathlete
Messages
31
Reaction score
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]);
 
Physics news on Phys.org
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
 
Triathlete said:
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   Reactions: BvU
Thanks for the responses!
How would I convert to matrices?
 
Triathlete said:
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.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K