Resolving MATLAB Plotting Issue with Velocity & Position

In summary, the code is trying to assign 3 values to a spot in a vector, but it is not possible because t is a decimal value. Once you convert t to an integer, you will also get an error because you can't assign 3 values to 1 spot in a vector.
  • #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]);
 
Physics news on Phys.org
  • #2
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
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 BvU
  • #4
Thanks for the responses!
How would I convert to matrices?
 
  • #5
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.
 

1. How do I plot velocity and position data in MATLAB?

To plot velocity and position data in MATLAB, you can use the plot function. Make sure to have two arrays, one for the time values and one for the corresponding velocity or position values. Then, use the command plot(time, velocity) or plot(time, position) to create the plot.

2. Why is my velocity and position plot not showing up?

There could be several reasons for this issue. First, make sure that you have correctly defined and assigned values to your time, velocity, and position arrays. Additionally, check that you have used the plot function correctly, and that you have also included the figure and hold commands to create a new figure and preserve the current plot, respectively.

3. How can I add labels and a title to my velocity and position plot?

To add labels and a title to your plot, use the xlabel, ylabel, and title functions. These commands allow you to specify the labels and title of your plot, and they should be placed after the plot function.

4. Can I plot both velocity and position on the same plot in MATLAB?

Yes, you can plot both velocity and position on the same plot in MATLAB by using the plot function twice. First, use the command plot(time, velocity) to create a plot for the velocity data. Then, use the command plot(time, position) to add the position data to the same plot. Don't forget to include labels and a title for your plot.

5. How can I change the appearance of my velocity and position plot in MATLAB?

To change the appearance of your plot, you can use various commands such as color, linestyle, and linewidth. These commands allow you to change the color, line style, and thickness of your plot, respectively. You can also add a grid using the grid command. Experiment with different options to customize your plot to your liking.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
987
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
Replies
5
Views
354
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
Back
Top