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

조회 수: 1 (최근 30일)
Adam Hermon
Adam Hermon 2019년 9월 23일
답변: Walter Roberson 2019년 9월 23일
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