필터 지우기
필터 지우기

How do I fix an array which is incompatible in sizes

조회 수: 1 (최근 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 CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by