Index Exceeds number of array elements (1) (Line 19)
1 view (last 30 days)
Show older comments
Adam Hermon
on 23 Sep 2019
Answered: Walter Roberson
on 23 Sep 2019
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
0 Comments
Accepted Answer
Walter Roberson
on 23 Sep 2019
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 Comments
More Answers (0)
See Also
Categories
Find more on Mathematics and Optimization in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!