Help understanding the error on 2-d ballistic trajector
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
J.A. Smith
le 8 Fév 2015
Réponse apportée : Image Analyst
le 8 Fév 2015
I am receiving the following error when attempting to plot the trajectory of a 1 kg mass starting at the same initial velocity from multiple angles
Attempted to access x2(103); index out of bounds because numel(x2)=1.
Error in assignment3 (line 137) x2(i) = x2(i-1) + vx2(i-1)*delta_t2;
The program will plot if only one loop is going but if I include the loop for other angles the error occurs. I need help understanding the problem. The code is posted below.
% Clear clear all
% Prompt the user v_int = input('Provide a value for intial veloctiy in meters per second: ');
% Define Variables
m = 1;
B = 4*10^(-5);
g = 9.8;
% Theta in degrees theta1 = 15; theta2 = 30; theta3 = 45; theta4 = 60; theta5 = 75;
% Convert to radians rad1 = 15*((2*pi)/360); rad2 = 30*((2*pi)/360); rad3 = 45*((2*pi)/360); rad4 = 60*((2*pi)/360); rad5 = 75*((2*pi)/360);
% Initialize variables
i = 1; time(1) = 0;
x1(1) = 0; y1(1) = 0; vx1(1) = cos(rad1)*v_int; vy1(1) = sin(rad1)*v_int; time_final1 = 2*((sin(rad1)*v_int)/(g)); delta_t1 = time_final1 / 100;
x2(1) = 0; y2(1) = 0; vx2(1) = cos(rad2)*v_int; vy2(1) = sin(rad2)*v_int; time_final2 = 2*((sin(rad2)*v_int)/(g)); delta_t2 = time_final2 / 100;
x3(1) = 0; y3(1) = 0; vx3(1) = cos(rad3)*v_int; vy3(1) = sin(rad3)*v_int; time_final3 = 2*((sin(rad3)*v_int)/(g)); delta_t3 = time_final3 / 100;
x4(1) = 0; y4(1) = 0; x4(1) = cos(rad4)*v_int; vy4(1) = sin(rad4)*v_int; time_final4 = 2*((sin(rad4)*v_int)/(g)); delta_t4 = time_final4 / 100;
x5(1) = 0; y5(1) = 0; vx5(1) = cos(rad5)*v_int; vy5(1) = sin(rad5)*v_int; time_final5 = 2*((sin(rad5)*v_int)/(g)); delta_t5 = time_final5 / 100;
% Calculate excluding drag % while y1 >= 0 i = i + 1; time(i) = time(i-1) + delta_t1; B = 0; x1(i) = x1(i-1) + vx1(i-1)*delta_t1; vx1(i) = vx1(i-1) - ((B*v_int*vx1(i-1))/(m))*delta_t1; y1(i) = y1(i-1) + vy1(i-1)*delta_t1; vy1(i) = vy1(i-1) - g*delta_t1 - ((B*v_int*vy1(i-1))/(m))*delta_t1; v(i) = SQRT(vx1(i)*vx1(i) + vy1(i)*vy1(i)); end
while y2 >= 0 i = i + 1; time(i) = time(i-1) + delta_t2; B = 0; x2(i) = x2(i-1) + vx2(i-1)*delta_t2; vx2(i) = vx2(i-1) - ((B*v_int*vx2(i-1))/(m))*delta_t2; y2(i) = y2(i-1) + vy2(i-1)*delta_t2; vy2(i) = vy2(i-1) - g*delta_t2 - ((B*v_int*vy2(i-1))/(m))*delta_t2; v(i) = SQRT(vx2(i)*vx2(i) + vy2(i)*vy2(i)); end
while y3 >= 0 i = i + 1; time(i) = time(i-1) + delta_t3; B = 0; x3(i) = x3(i-1) + vx3(i-1)*delta_t3; vx3(i) = vx3(i-1) - ((B*v_int*vx3(i-1))/(m))*delta_t3; y3(i) = y3(i-1) + vy3(i-1)*delta_t3; vy3(i) = vy3(i-1) - g*delta_t3 - ((B*v_int*vy3(i-1))/(m))*delta_t3; v(i) = SQRT(vx3(i)*vx3(i) + vy3(i)*vy3(i)); end
while y4 >= 0 i = i + 1; time(i) = time(i-1) + delta_t4; B = 0; x4(i) = x4(i-1) + vx4(i-1)*delta_t4; vx4(i) = vx4(i-1) - ((B*v_int*vx4(i-1))/(m))*delta_t4; y4(i) = y4(i-1) + vy4(i-1)*delta_t4; vy4(i) = vy4(i-1) - g*delta_t4 - ((B*v_int*vy4(i-1))/(m))*delta_t4; v(i) = SQRT(vx4(i)*vx4(i) + vy4(i)*vy4(i)); end
while y5 >= 0 i = i + 1; time(i) = time(i-1) + delta_t5; B = 0; x5(i) = x5(i-1) + vx5(i-1)*delta_t5; vx5(i) = vx5(i-1) - ((B*v_int*vx5(i-1))/(m))*delta_t5; y5(i) = y5(i-1) + vy5(i-1)*delta_t5; vy5(i) = vy5(i-1) - g*delta_t5 - ((B*v_int*vy5(i-1))/(m))*delta_t5; v(i) = SQRT(vx5(i)*vx5(i) + vy5(i)*vy5(i)); end
plot(x1, y1, x2, y2, x3, y3, x4, y4, x5, y5) axis('auto') title('Ballistic Trajectories Neglecting Drag at v = ') xlabel('x-distance (meters)') ylabel('y-distance (meters)') legend('theta = 15 degrees','theta = 30 degrees','theta = 45 degrees','theta = 60 degrees','theta = 75')
1 commentaire
Image Analyst
le 8 Fév 2015
Read this: http://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup Then fix your post.
Because you have SQRT in there and that is not a MATLAB function (though sqrt is), and consequently your program does not run, I feel like you didn't give us your actual code. Please attach your actual file with the paper clip icon so people can run it.
Réponse acceptée
Plus de réponses (2)
Erik S.
le 8 Fév 2015
Hi You need to reset your counter i in between the loops. When the fist while loop finish (y2>=0 condition is met) the value of i is 102. When you enter the second while you increment one more time and get i 103, which causes you error.
Reset i between the loops or use different variables for loop counting.
Image Analyst
le 8 Fév 2015
You had a bunch of errors in there. I fixed them, so see fixed m-file below the image.
0 commentaires
Voir également
Catégories
En savoir plus sur Lighting, Transparency, and Shading dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!