index exceeds matrix dimensions error

Here is some code i've been working on and i keep getting the "index exceeds matrix dimension" error
%Calculate Trajectory
v = 200;
theta = 20;
g = 32.2;
i = 0;
t(1) = 0;
h(1) = 0;
x(1) = 0;
while(h >= 0)
i=i+1;
h(i) = v.*(t(i)).*sind(theta)-1/2.*g.*(t(i)).^2;
x(i) = v.*(t(i)).*cosd(theta);
t(i) = t + .1;
h=h(i);
end

 채택된 답변

Sean de Wolski
Sean de Wolski 2012년 2월 13일

0 개 추천

After the first iteration of the loop, x,h,t, are all still scalars and you're going to try and refernce their second element (i==2). That index exceeds the dimension and you get the above error.

댓글 수: 1

Benjamin Lynch
Benjamin Lynch 2012년 2월 13일
ok thank you, how do i initialize x,h,and t so that they are vectors and the first element in all of them is 0?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 2월 13일

0 개 추천

v = 200;
theta = 20;
g = 32.2;
i = 0;
t(1) = 0;
h(1) = 0;
x(1) = 0;
while(h(end) >= 0)
i=i+1;
h(i+1) = v.*(t(i)).*sind(theta)-1/2.*g.*(t(i)).^2;
x(i+1) = v.*(t(i)).*cosd(theta);
t(i+1) = t + 0.1;
end

댓글 수: 2

Benjamin Lynch
Benjamin Lynch 2012년 2월 13일
when i plug that code in i get this error:
In an assignment A(I) = B, the number of elements in B and I must be the same.
Error in trajectory (line 24)
t(i+1) = t + .1;
Srinivas
Srinivas 2012년 2월 13일
change this line
t(i+1) = t(i) + 0.1;

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by