My code keeps coming up with "Index Exceeds Array Bounds" help?

조회 수: 2(최근 30일)
Madison Goodwin
Madison Goodwin 2018년 4월 30일
답변: VBBV 2021년 10월 25일
n = 0;
t = 0;
deltat = 0.1;
m = 0.44;
Cd = 0.05;
p = 1.29;
Ra = 0.135;
%Find b using the rearranged formula for circumference of a circle.
b = 0.6/(pi()*2);
A = pi()*Ra*b;
% Pre set random values for testing code
x = [12,12,12];
deltat = [0.1];
agravity = [0,0,-9.81];
v = [2,2,2];
vwind = [3,1,2];
vdrag = [2,1,2];
fdrag = [3,2,3];
while x<=100
n = n + 1;
t(n+1) = t(n) + deltat;
x(n+1) = x(n) + v(n).*deltat;
v(n+1) = v(n) + (agravity(n).*deltat);
end

답변(2개)

Walter Roberson
Walter Roberson 2018년 4월 30일
You have
agravity = [0,0,-9.81];
so agravity is length 3. You are not storing new agravity entries as you go along like you are for t, x, v, so when you reach n = 4, agravity(n) will be an error.
  댓글 수: 2
Madison Goodwin
Madison Goodwin 2018년 4월 30일
So I want to create arrays for each variable such that its got x,y,z quantities. So I have to create a loop that iterates over each value in the array. But I also need to include in that the time is also changing in the loop, and I don't know how? But ive got this so far. Where: t = 0 n = 0 deltat = 0.1,
x = [], v = [], fdrag = [], vwind = [], vdrag = [], agravity= []
while x<=100
n = n + 1;
t(n+1) = t(n) + deltat;
x(n+1) = x(n) + (v(n).*deltat);
% Finding Acceleration including other forces
vdrag = v(n) - vwind(n);
Magfdrag = norm(fdrag(n));
Magfdrag(n) = 0.5*p*A*Cd.*(norm(vdrag(n))).^2;
fdrag(n+1) = Magfdrag(n).*(-vdrag(n)/norm(vdrag(n)));
atotal(n+1) = agravity(n) + (fdrag(n)./m);
v(n+1) = v(n) + (atotal(n).*deltat);
end

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


VBBV
VBBV 2021년 10월 25일
n = 1;
t = 0;
deltat = 0.1;
m = 0.44;
Cd = 0.05;
p = 1.29;
Ra = 0.135;
%Find b using the rearranged formula for circumference of a circle.
b = 0.6/(pi()*2);
A = pi()*Ra*b;
% Pre set random values for testing code
x = [12,12,12];
deltat = [0.1];
agravity = -9.81;
v = [2,2,2];
vwind = [3,1,2];
vdrag = [2,1,2];
fdrag = [3,2,3];
while x(n)<=100
t(n+1) = t(n) + deltat;
x(n+1) = x(n) + v(n).*deltat;
v(n+1) = v(n)-(agravity.*deltat);
n = n + 1;
end
v
v = 1×42
2.0000 2.9810 3.9620 4.9430 5.9240 6.9050 7.8860 8.8670 9.8480 10.8290 11.8100 12.7910 13.7720 14.7530 15.7340 16.7150 17.6960 18.6770 19.6580 20.6390 21.6200 22.6010 23.5820 24.5630 25.5440 26.5250 27.5060 28.4870 29.4680 30.4490
Try this

Community Treasure Hunt

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

Start Hunting!

Translated by