Index Exceeds number of array elements (1) (Line 19)

1 次查看(过去 30 天)
Hi, so i am new to Matlab and I am trying to do an assignment that solves for an unknown mass in a elastic collision. Everytime I run the equation that I have derived, I get the error "Index exceeds number of array elements (1)" on the line solving for m1. Any solution would be appreciated, thanks so much.
clear % clears all variables in the Workspace, avoids common errors
% ----- given information -----
m2 = 150; % mass of car 2, in g
v1i = 30; % initial velocity of car 1, in cm/s
v2i = -30; % initial velocity of car 2, in cm/s
v1f = 0; % the final velocity wanted for car 1 after collision, in cm/s
% ----- calculations -----
m1 = m2(v1i + v1f - 2 * v2i) / (v1i - v1f) % mass of first car, derived
% from equation on paper
M = m1+m2 % total mass of both cars, in g

采纳的回答

Walter Roberson
Walter Roberson 2019-9-23
I would suggest that when you wrote
m2(v1i + v1f - 2 * v2i)
that you were thinking of multiplication between m2 and (v1i + v1f - 2 * v2i) . However, MATLAB has no implicit multiplication anywhere (that I can think of); the expression A(B) in MATLAB means either array A indexed at locations given by B, or else a function named A called with the single parameter B. MATLAB uses .* and * as the multiplication operators, with .* being used for element-by-element multiplication, and * being used for algebraic matrix multiplication.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Mathematics and Optimization 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by