How do I fix an array which is incompatible in sizes

2 ビュー (過去 30 日間)
Thomas
Thomas 2023 年 10 月 7 日
コメント済み: Walter Roberson 2023 年 10 月 7 日
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 Center および File ExchangeHistorical Contests についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by