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

조회 수: 1 (최근 30일)
Madison Goodwin
Madison Goodwin 2018년 4월 30일
댓글: Madison Goodwin 2018년 4월 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일
How do I change this? because im meant to be simulating a kick? This is currently what I have, so each value should update as time goes on but I'm not quite sure how to do this? So xx is distance in x , XY is distance in y, XZ is distance in Z. but I want to make this code shorter and nicer, which above I posted my vectorised code
XX(n+1) = XX(n) + (VX(n))*deltat;
vdragX(n) = VX(n)- vwindX;
MagfdragX = norm(fdragX);
MagfdragX(n) = 0.5*p*A*Cd*(norm(vdragX(n)))^2;
fdragX(n) = MagfdragX(n)*(-vdragX(n)/(norm(vdragX(n))));
atotalX(n) = agravityX + fdragX(n)/m;
VX(n+1) = VX(n) + (agravityX*deltat);
% Y axis
XY(n+1) = XY(n) + (VY(n))*deltat;
vdragY(n) = VY(n)- vwindY;
MagfdragY = norm(fdragY);
MagfdragY(n) = 0.5*p*A*Cd*(norm(vdragY(n)))^2;
fdragY(n) = MagfdragY(n)*(-vdragY(n)/(norm(vdragY(n))));
atotalY(n) = agravityY + fdragY(n)/m;
VY(n+1) = VY(n) + (agravityY*deltat);
%Distance & Velocity in z axis direction with drag forces and wind
XZ(n+1) = XZ(n) + (VZ(n))*deltat;
vdragZ(n) = VZ(n)- vwindZ;
MagfdragZ = norm(fdragZ);
MagfdragZ(n) = 0.5*p*A*Cd*(norm(vdragZ(n)))^2;
fdragZ(n) = MagfdragZ(n)*(-vdragZ(n)/(norm(vdragZ(n))));
atotalZ(n) = agravityZ + fdragZ(n)/m;
VZ(n+1) = VZ(n) + (atotalZ(n)*deltat);
end
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

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

카테고리

Help CenterFile Exchange에서 Particle & Nuclear Physics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by