Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB plotting issue

  1. Oct 21, 2015 #1
    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;

    axis([1 5400 0 10000]);
    axis([1 5400 0 5]);
  2. jcsd
  3. Oct 21, 2015 #2


    User Avatar
    Science Advisor
    Homework Helper
    2017 Award

    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)


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

    but hen in matlab language
  4. Oct 21, 2015 #3


    User Avatar

    Staff: Mentor

    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.
  5. Oct 21, 2015 #4
    Thanks for the responses!
    How would I convert to matrices?
  6. Oct 22, 2015 #5


    User Avatar
    Gold Member

    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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Threads - MATLAB plotting issue Date
Matlab Plotting Coordinate Transformations in Matlab Feb 12, 2018
Matlab Plotting animations (time-evolution) Jan 17, 2018
Does anyone had this plotting problem with MATLAB? Dec 23, 2017
Matlab Matlab's plot command Nov 30, 2017
Matlab Matlab -- how to make a smooth contour plot? Jun 28, 2017