Clear Filters
Clear Filters

How do I fix an array which is incompatible in sizes

1 view (last 30 days)
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 Comment
Walter Roberson
Walter Roberson on 7 Oct 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

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 7 Oct 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 Comment
Walter Roberson
Walter Roberson on 7 Oct 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.

Sign in to comment.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!