Running a kinematics while loop containing an if loop. I get the error: Index exceeds number of array elements(1).
Show older comments
h(1)=150000; %initial height
a(1)=(40*10^7)/(6371+h(1))^2; %initial acceleration dependant on height
dt=0.005; %time step
t(1)=0; %initial time
v(1)=a(1)*t(1); %velocity
g(1)=((40*10^7)/(6371+h(1))^2); %Downward force h>100000, upward force = 0 above 100000
As= 5; %Area
m=850; %Mass
c=0.7;
p(1)=(100000/71)+1.4; % Initial Air Density (Air density occurs at h=100000, from then p=(h/71)+1.4)
Fd(1)=0.5*p(1)*c*As*v^2; %Downward force h<=100000
i=1; %loop counter
while h(end)>=0
t(i+1)=t(i)+dt;
h(i+1)=150000-(a(i+1)*(t(i+1))^2);
if h(i+1)>100000
g(i+1)=(40*10^7)/(6371+h(i+1))^2;
a(i+1)=g(i+1);
else
p(i+1)=((h(i+1))/71)+1.4;
Fd(i+1)=0.5*(p(i+1))*c*As*(v(i+1))^2;
g(i+1)=(40*10^7)/(6371+h(i+1))^2;
a(i+1)=(g(i+1))-((Fd(i+1))/m);
end
v(i+1)=(a(i+1))*(t(i+1));
i=i+1;
end
I have posted this before and got some very useful advice but it did not fully fix the problem. Any help would be appreciated.
Answers (1)
Sriram Tadavarty
on 12 Mar 2020
Hi Sam,
In the second statement of while loop, the variable a is indexed with value of 2, before setting the value of it. This is the source of error.
h(i+1)=150000-(a(i+1)*(t(i+1))^2); % Before this execution, make sure the varaibles a and t, have a value at 'i+1' index
From the code it is not pretty clear as what you intended to do. More details on problem statement would help.
But, based on code comments and some assumptions, i can suggest some modifications to the code in loop:
- Update the height and velocity before the increment time
- Index the variable with one step before the increment time
- Then increment the time
while h(end)>=0
v(i+1)=(a(i))*(t(i)); % Find the velocity of previous time increment
h(i+1)=150000-(a(i)*(t(i))^2); % Find the height of previous time increment
t(i+1)=t(i)+dt; % Update the time step
if h(i+1)>100000
g(i+1)=(40*10^7)/(6371+h(i+1))^2;
a(i+1)=g(i+1);
p(i+1)=p(1); % Set this value to some default value, chosen as the initial value (Since this is used directly with the indexing of 'i+1' in the else statement)
Fd(i+1)=Fd(1); % Same reason as p
else
p(i+1)=((h(i+1))/71)+1.4;
Fd(i+1)=0.5*(p(i+1))*c*As*(v(i+1))^2;
g(i+1)=(40*10^7)/(6371+h(i+1))^2;
a(i+1)=(g(i+1))-((Fd(i+1))/m);
end
i=i+1;
end
The udpate of your code with above code, will remove any errors that you see.
Hope this helps.
Regards,
Sriram
17 Comments
Sam Potter
on 14 Mar 2020
Sriram Tadavarty
on 14 Mar 2020
May i know, if the previous while loop code worked?
If yes, the same can be applied here
Sam Potter
on 14 Mar 2020
Sriram Tadavarty
on 14 Mar 2020
Thanks for the update Sam.
The errors comes from the first line of while loop, which index'es second element of variables 'a' and 't'. Like a(i+1) => a(2) which is not defined, similarly t(i+1) => t(2) also not defined. So, the error.
Update 'a' and 't' such that those values are known before. This should solve that error.
Sam Potter
on 14 Mar 2020
Sriram Tadavarty
on 14 Mar 2020
Sam, your code only knows a(1) and t(1), but in the first line of while loop
v(i+1)=(a(i+1))*(t(i+1));
% Here you are accessing a(2) and t(2), which is not known (Since i is 1 before the while loop)
Hope you can see the issue now. As second element of matrix a is accessed without having that value, MATLAB errors.
So, i advice you to perform the loop from initial value and then update the time step. I could help more, if i know the exact equations.
Sam Potter
on 14 Mar 2020
Walter Roberson
on 14 Mar 2020
Fd(i+1)=0.5*(p(i+1))*c*As*(v(i+1))^2;
You do not assign to v(i+1) until 3 lines further down.
Sriram Tadavarty
on 14 Mar 2020
Yes. If the update to v is placed ahead of update to Fd, the issue will go away.
% In the else statement
% you can try placing the update of v ahead with previous acceleration value
p(i+1)=((h(i+1))/71)+1.4;
v(i+1)=59.29+(a(i))*(t(i+1));
Fd(i+1)=0.5*(p(i+1))*c*As*(v(i+1))^2;
g(i+1)=(40*10^7)/(6371+h(i+1))^2;
a(i+1)=(g(i+1))-((Fd(i+1))/m);
% Or you can just update the Fd value with previous v
Fd(i+1)=0.5*(p(i+1))*c*As*(v(i))^2;
Either of the two should work, but it depends on what is required there.
Sam Potter
on 14 Mar 2020
Edited: Walter Roberson
on 15 Mar 2020
Sriram Tadavarty
on 15 Mar 2020
Hi Sam, It seems like the way equations are formulated to code, went wrong then. May I know what are the expected graphs for (t,v) and (t,a)? It would be helpful to easily find out what went wrong by writing the equations for the first three time steps in notes and see if the code is behaving as such, as a first step. Regards Sriram
Sam Potter
on 15 Mar 2020
Sam Potter
on 16 Mar 2020
Sriram Tadavarty
on 16 Mar 2020
Yes Sam. Even, i did observe it. But, I am not sure what your intended code should do.
Sam Potter
on 16 Mar 2020
Sriram Tadavarty
on 16 Mar 2020
Thanks for the explanation of what is intended. Yes Sam, a new while loop would benefit. I see that your update to velocity is not updating properly, since u is ignored. For only first time step u is 0, for all other time steps, it must be the previous value. Right?
Sam Potter
on 16 Mar 2020
Categories
Find more on Loops and Conditional Statements 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!