How do I fix an array which is incompatible in sizes

7 次查看(过去 30 天)
Question I'm attempting:
*Write a script to determine the velocity components vx and vy as well as the magnitude of the velocity between t = 0 and t = 10 seconds
*Plot these three velocities on a single plot and include all labelling and a legend.
userInput = input('1000');
v0 = 231004 \ userInput;
Ay = 9810 \ userInput;
disp(['Initial velocity (v0): ', num2str(v0), 'm/s']);
disp(['Vertical acceleration (Ay): ', num2str(Ay), 'm/s^2']);
% Formulas for calculations
t = 0:0.5:10;
d = 100;
Vy = v0 - Ay;
Vx = d\t;
V_Mag = sqrt((Vy.^2) + (Vx.^2));
Error using +
Arrays have incompatible sizes for this operation.
Error in Help_7_10_23 (line 12)
V_Mag = sqrt((Vy.^2) + (Vx.^2))
figure;
Plot(t, Vy);
legend ('Vertical Velocity');
hold on
plot(t,Vx);
legend ('Horizontal Velocity');
hold on
Plot(t,V_Mag);
legend ('The magnitude of velocity');
Hold off
title ('Vertical and Horzontal velocity');
xlabel ('Time (s)');
ylabel ('Velocity (m/s)');
  1 个评论
Walter Roberson
Walter Roberson 2023-10-7
Why are you using the mldivide, \ operator for operations between scalars and vectors? P\Q with scalar P is a obscure way to write Q./P

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2023-10-7
You are entering a non-scalar in response to the input('1000') prompt, and whatever size it is that you are entering is not the same size as
t = 0:0.5:10;
Your Vy is the same size as your t but your Vx is the same size as what was entered for the input() and those are probably not going to be the same size.
  1 个评论
Walter Roberson
Walter Roberson 2023-10-7
You really need better comments, or better variable names.
userInput = input('1000');
What's that? A rotational velocity in rpm ? An acceleration in furlongs per fortnight per jiffy ? An area in square hectares of the cross-section of a pistion ?
People reading your code should be able to understand your code. Unless you are entering the International Obfuscated C contest https://www.ioccc.org/ there is no point in writing code that cannot be understood.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Historical Contests 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by