Keep on getting 'Error using plot, Vectors must be the same length'. How can I plot it correctly?

조회 수: 9 (최근 30일)
t(1) = 0; %Time, seconds
T(1) = 0; %Thrust, Newtons
m = 85/1000; % mass, grams
h(1) = 0.2; %Height, meters
A_rocket = 1.2141; %Drag Area, meters^2
c_d = 0.06; %Coefficient of Drag
g = 9.81; %Gravity, meters/second^2
p = 1.225; %Air Density, kilograms/meter^3
v(1) = 0; %Velocity, meters/second
a(1) = 0; %Acceleration, meters/second^2
W = 0.324; %Weight, Newtons
%%
i = 1; %Loop Term
h_check = h(1); %Height Check
t_step = 0.01; %Time Step Intervals
while h_check >= -0.5
%Thrust Function
if t(i) <= 0.22
T(i) = 45.45*(t(i));
elseif t(i) <= 0.27
T(i) = -140*(t(i))+3;
elseif t(i) <= 0.67
T(i) = -2.5*(t(i))+3;
elseif t(i) <= 0.72
T(i) = -40*(t(i))+2;
else T(i) = 0;
break
end
a(i+1)= - m * g + T(i) - c_d * ((p * v(i).^2)/2) * 0.2 / m
v(i + 1) = v(i) + a(i) * t_step
h(i + 1) = h(i) + v(i) * t_step
t(i+1) = t(i)+t_step;
i = i + 1
end
t(end) = [];
h(end) = [];
figure
plot(t,T)
title('Time Vs. Thrust')
xlabel('Time')
ylabel('Thrust')
figure
plot(t,a)
title('Time Vs. Acceleration')
xlabel('Time')
ylabel('Acceleration')
figure
plot(t,v)
title('Time Vs. Velocity')
xlabel('Time')
ylabel('Velocity')
figure
plot(t,h)
title('Time Vs. Height')
xlabel('Time')
ylabel('Height')
  댓글 수: 2
KSSV
KSSV 2020년 12월 8일
To plot the x-data and y-data should be of same dimension.
We cannot run your code to check as most of the variables are not defined.
Adam
Adam 2020년 12월 8일
Just check that the vectors you are plotting are the same length as each other. It's much easier for you to do that when you have the variables there in your workspace than for us looking at some ill-formatted code on screen. t must be the same length as T, a, v and h.

댓글을 달려면 로그인하십시오.

채택된 답변

Daniel Pollard
Daniel Pollard 2020년 12월 8일
편집: Daniel Pollard 2020년 12월 8일
When the while loop breaks, you have defined T to have length of i_max, and t has length i_max+1. This is because of the line
t(i+1) = t(i) + t_step
which causes the vector t to be one element longer than T. To be able to plot correctly, you could either drop the last element in t (since it doesn't appear to affect the last element of T) or compute a final element of T. It looks like you already thought of my first idea, so if you remove the lines
t(end) = [];
h(end) = [];
your code should work.
Edit I made some errors in my previous answer, this is corrected now.
  댓글 수: 4
Daniel Pollard
Daniel Pollard 2020년 12월 8일
Excellent, glad to hear it. In that case accept the answer and close the question, so that people who search in the future know what worked for you.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2020년 12월 8일
Looking at a portion of your code with other parts cut out:
% snip
while h_check >= -0.5
% snip
a(i+1)= - m * g + T(i) - c_d * ((p * v(i).^2)/2) * 0.2 / m
v(i + 1) = v(i) + a(i) * t_step
h(i + 1) = h(i) + v(i) * t_step
t(i+1) = t(i)+t_step;
% snip
end
At this point, the vectors a, v, h, and t are likely the same length assuming that the lines where you assign to element i+1 of each are actually growing the corresponding vector.
t(end) = [];
h(end) = [];
t and h are now one element shorter than a and v.
% snip
figure
plot(t,a)
% snip the rest of the code
These two vectors are not the same length so MATLAB correctly throws an error.
FYI for the future, one tool that can be useful in debugging this type of error is an error breakpoint. This will cause MATLAB to stop as soon as the error occurs so you can examine the size, type, and contents of the variables being used on the line where the error occurs.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by