MATLAB Answers

Error using plot Vectors must be the same lengths when plotting thetap_dot(k) vs. time.

4 views (last 30 days)
Jenna Le
Jenna Le on 13 Sep 2020
Commented: Walter Roberson on 14 Sep 2020
dt= 0.001; % sampling interval [s]
N= ((T_on+T_off)/dt)+1; % how many "snapshots" in time
t= (0:dt:(N-1)*dt); % duration
xdot(1)= 2/2.23694; % initial velocity [m/s]
for k= 1:(N-1)
if xdot(k)< 10.2771045561311
thetap_dot(k)= (1/Rt)*GR*xdot(k);
xddot(k)= ((GR/Rt)*(P/thetap_dot(k)))/(M+2*It/Rt^2);
else
thetap_dot(k)= (1/Rt)*GR*xdot(k);
xddot(k)= ((GR/Rt)*(0/thetap_dot(k) ))/(M+2*It/Rt^2);
end
xdot(k+1)= xdot(k)+ xddot(k)*dt;
end

Answers (2)

Vasishta Bhargava
Vasishta Bhargava on 13 Sep 2020
Edited: Walter Roberson on 13 Sep 2020
for k = 1: N
...
end

  5 Comments

Show 2 older comments
Walter Roberson
Walter Roberson on 14 Sep 2020
length(t) will be N
for k = 1 : N, xdot(k+1) will assign up to xdot(N+1)
plot() of something length N by something of length N+1 will not work.
You could loop only to N-1 so you assign to xdot((N-1)+1) -> xdot(N) and then xdot and t would have the same size. But in your loop you assign to thetap_dot(k) not to thetap_dot(k+1) so thetap_dot is always going to be one shorter than xdot .
Vasishta Bhargava
Vasishta Bhargava on 14 Sep 2020
N = ((T_on+T_off)/dt)+1;
t = linspace(0,round((N-1)*dt),round(N));
for k = 1:length(t)
...
end
figure (1)
plot(t,xdot*2.23694,'linewidth',2);
figure (2)
plot(t,((thetap_dot)/(2*pi/60)),'linewidth',2);
Walter Roberson
Walter Roberson on 14 Sep 2020
If you define
N = round(((T_on+T_off)/dt)+1);
then you can skip the round() in the linspace() and you can use N as the upper bound of the for loop.
However you will still need to deal with the fact that xdot and thetap_dot will inherently be different sizes for this calculation. For the longer one, you need to decide whether to plot the first N points or the last N points.

Sign in to comment.


Walter Roberson
Walter Roberson on 13 Sep 2020
dt= 0.001; % sampling interval [s]
N= ((T_on+T_off)/dt)+1; % how many "snapshots" in time
N will not usually be an integer, even if T_on and T_off are integers. Remember that 0.001 is not exactly representable in binary floating point, so division by 0.001 is not the same thing as multiplication by 1000, and we as outside observers have no reason to expect that T_on and T_off are integer valued.
With N not usually being an exact integer even if 1000*(T_on+T_off) would be an integer, it becomes difficult to predict the length of
t= (0:dt:(N-1)*dt); % duration
Due to round-off in floating point calculations, this could end up one item shorter than it looks like it "should" be.
You should probably define N as an integer using round() and then use
t = (0:N-1)*dt

  3 Comments

Jenna Le
Jenna Le on 13 Sep 2020
hello! even with using the round function for defining N and chancing the N value to be 1000*(T_on+T_off) it still is coming up with the same error.
Vasishta Bhargava
Vasishta Bhargava on 14 Sep 2020
N = ((T_on+T_off)/dt)+1;
%if mod(N,1) ~= 0
%N = floor(N) ;
%else
% N = N;
%end
t = linspace(0,ceil((N-1)*dt),floor(N));
for k = 1:length(t)
...
end
figure (1)
plot(t,xdot*2.23694,'linewidth',2);
figure (2)
plot(t,((thetap_dot)/(2*pi/60)),'linewidth',2);
Use floor or ceil functions after checking using mod operation

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by