CANT FIGURE OUT AN ERROR MESSAGE

1 view (last 30 days)
Oliver Stericker-Taylor
Oliver Stericker-Taylor on 7 Apr 2020
Edited: matquest on 8 Apr 2020
so im making a model that follows an object in freefall and keep getting the same error message: "Index exceeds the number of array elements (1)." i really dont know what ive done wrong can someone please tell me? my code is below
promptA = 'What is the mass of the Spacecraftt in Kg? ';
M = input(promptA);
promptB = 'What is the Cross Sectional Area of the Spacecraft in m^2? ';
As = input(promptB);
promptC = 'What is the Cross Sectional Area of the Parachute? ';
Ap = input(promptC);
promptD = 'What is the Initial Height of the Spacecraft in km? ';
Hs = input(promptD);
promptE = 'What is the parachute deployment height in km? ';
Hp = input(promptE);
promptF = 'What is the initial velocity of the Spacecraft in m/s? ';
U = input(promptF);
%Constants
C=0.7; % Drag Coefficient
Ha=100; % Height of the Atmosphere in km
Tstep=0.1; % Time step in seconds
% Initialisation of Increments
i=1;
H(i)=Hs;
V(i)=0;
S(i)=0;
t(i)=0;
% Freefall above the atmosphere
while (H(i)>Ha)
Fd=0;
g(i)=(40*(10^7))/(6371+H(i));
a(i)=g(i);
V(i+1)=V(i)+(a(i)*Tstep); % Velocity at next i
S(i+1)=S(i)+(V(i)*Tstep+0.5*a(i)*Tstep^2); % Displacement at next i
t(i+1)=t(i)+Tstep; % Time at next incriment
i=i+1;
continue
end
% Freefall within the Atmosphere (No parachute)
A=As;
while (H(i)>Hp)
p(i)=(H(i)/71)+1.4; % Drag Coefficient for Fd
Fd(i)=(0.5*C*p(i)*A*(V(i)^2)); % Air Resistance
a(i)=(g(i)-Fd(i)/M); % New Acceleration formula inc. Fd
V(i+1)=V(i)+(a(i)*Tstep);
S(i+1)=S(i)+(V(i)*Tstep+0.5*a(i)*Tstep^2);
t(i+1)=t(i)+Tstep;
i=i+1;
continue
end
% Freefall within the Atmosphere with a parachute
A=Ap;
while (H(i)>0)
p(i)=(H(i)/71)+1.4; % Drag Coefficient for Fd
Fd(i)=(0.5*C*p(i)*A*(V(i)^2)); % Air Resistance
a(i)=(g(i)-Fd(i)/M); % New Acceleration formula inc. Fd
V(i+1)=V(i)+(a(i)*Tstep);
S(i+1)=S(i)+(V(i)*Tstep+0.5*a(i)*Tstep^2);
t(i+1)=t(i)+Tstep;
i=i+1;
continue
end
  3 Comments
Steven Lord
Steven Lord on 8 Apr 2020
When you run your code, MATLAB displays a bunch of text in red (and maybe some text in orange) in the Command Window. Can you copy and paste all that red (errors) and/or orange (warnings) text into a comment? That will likely provide information that will help us determine where the problem occurs and how best to correct it.
And since you are new, I'm going to make my usual new user suggestion. On this page, click on the word Support at the top. On the Support page scroll down to the Tutorials section and click on View Tutorials. The MATLAB Onramp course available on that page is a free online two hour introduction (through videos and exercises) to the essentials of MATLAB that you may find useful.

Sign in to comment.

Answers (1)

matquest
matquest on 7 Apr 2020
Edited: matquest on 7 Apr 2020
After looking at your code a little more, I suspect you are getting this error either at line while(H(i) > Ha) or while (H(i) > Hp) or (H(i) > 0). Notice that inside both loops you do not check to ensure that i is not larger than length(H). You could add that to your while loop and it would probably fix the problem:
while (i <= length(H) && H(i) > Ha)
...
end
while (i <= length(H) && H(i) > Hp )
...
end
while (i <= length(H) && H(i)> 0)
...
end
Additionally, what is the continue doing inside both loops? Also, generally it's not recommended to use i or j as indices because matlab uses them as imaginary numbers; idx or index are self-explanatory and pretty common in other languages as well
  2 Comments
matquest
matquest on 8 Apr 2020
Edited: matquest on 8 Apr 2020
To answer the "easy" question:
An index is exactly what it sounds like - a reference to a position in something. In the case of programming, it's usually a list or array of items (e.g., an array of integers might look like this: [1, 2, 3, 5, 8, 13]; the value 8 is at index 5). When you're looping through an array the variable you're using (in your case it's i) keeps track of where you are in the array.
Why they recommend not using i or j as an index in matlab:
If you startup matlab and type i into the terminal, you should see this:
>>i
ans =
0.0000 + 1.0000i
This is because by default matlab assumes that i refers to the imaginary number i (the same thing is true for j). So instead they recommend that you use some other variable.
Note that this is not true (to my knowledge) in most programming languages (e.g., C, C++, java, etc) or scripting languages (python, bash, etc.) and you will often see i, j, and k used as indices in programs written in those languages.
You can read a much better and more thorough explanation on for loops and arrays on Wikipedia, but be sure to read the section on "Element identifier and addressing formulas" so you don't get confused about 0 vs 1-based indexing.
As to why you might be getting the same velocity each time:
Unfortunately, I am not a physicist, but I noticed that your variable H is just a single value. If you typed in 120 at the prompt, you'd get H = 120. If you think of H as being an array, it looks like this: [120]. There's only one value in it, so all of your while loops only execute one time.
Since you're dividing the problem into 3 stages (freefall above the atmosphere vs freefall within the atmosphere vs freefall within atmosphere w/ parachute), I think you should be checking the position. Based on the comments in your code, S is the "displacement" variable. Is that the distance that you've moved (fallen) in the timestep? If so, you could do this:
while (H > Ha) % notice we're not using an index - we're just checking the value of H
...
H = H - S(i+1); % you might need to do H= H - S(i); I am not sure which one is correct
...
end
Again, I'm not a physicist, so this is just a wild guess.
Edited to add: in case you weren't already aware of this, before you run your program, you can click the dash "-" next to any of the line numbers on the left side of your code. A little red circle should appear. This is a "breakpoint". If you run your code, it'll stop when it gets to that line. You can type into the matlab terminal and edit or inspect values in your variables. If the program doesn't stop on that line it means that it's not reaching that line for whatever reason. You can move a step at a time or run to the next breakpoint by click the "Continue" or "Step" buttons in the Editor toolbar at the top of your matlab session.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by