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

1 Ansicht (letzte 30 Tage)
Adam Hermon
Adam Hermon am 23 Sep. 2019
Beantwortet: Walter Roberson am 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

Akzeptierte Antwort

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

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics and Optimization finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by