Running a kinematics while loop containing an if loop. I get the error: Index exceeds number of array elements(1).

1 visualización (últimos 30 días)
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.

Respuestas (1)

Sriram Tadavarty
Sriram Tadavarty el 12 de Mzo. de 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 comentarios
Sriram Tadavarty
Sriram Tadavarty el 16 de Mzo. de 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
Sam Potter el 16 de Mzo. de 2020
No, (unless I am misunderstanding you), I believe u is constantly 0, whch is why I ignored it. One thing I forgot to mention is that I used the value of 59.29 in my else loop as that is the last value of v whilst h>100000.
This is the seperte while loop I have made:
>> clear
h(1)=100000;
t(1)=0;
dt=0.005;
u=59.29;
a(1)=0.03535;
v(1)=u+(a(1)*t(1)); %Initial velocity
p(1)=(((h(1))/71)+1.4); %Air Density
g(1)=(40*10^7)/((6371+h(1))^2); %initial gravity
A=5;
c=0.7;
m=850;
Fd(1)=0.5*c*(p(1))*A*(v(1))^2;
i=1;
while h(end)>=0
t(i+1)=t(i)+dt;
h(i+1)=100000-(u*t(i+1))-(0.5*a(i)*t(i+1)^2);
p(i+1)=(((h(i+1))/71)+1.4);
g(i+1)=(40*10^7)/((6371+h(i+1))^2);
Fd(i+1)=0.5*c*(p(i+1))*A*(v(i))^2;
a(i+1)=g(i+1)-(Fd(i+1)/m);
v(i+1)=u+(a(i+1)*t(i+1));
i=i+1;
end
The issue with this loop is that it seems to be infinite.

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by