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

조회 수: 1(최근 30일)
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

답변(1개)

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

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

범주

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

태그

Community Treasure Hunt

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

Start Hunting!

Translated by