Filter löschen
Filter löschen

How do I fix an array which is incompatible in sizes

1 Ansicht (letzte 30 Tage)
Thomas
Thomas am 7 Okt. 2023
Kommentiert: Walter Roberson am 7 Okt. 2023
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 Kommentar
Walter Roberson
Walter Roberson am 7 Okt. 2023
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

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Walter Roberson
Walter Roberson am 7 Okt. 2023
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 Kommentar
Walter Roberson
Walter Roberson am 7 Okt. 2023
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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by