Going from one while loop to another

Is there a way to go to another while loop when the condition's of the first loop have been met? For example:
clear
t(1)=0;
h(1)=150*10^3;
v(1)=0;
a(1)=(40*10^7)/(6371+(150*10^3))^2;
dt=0.1;
i=1;
while h >= 100*10^3
t(i+1) = t(i)+dt;
a(i+1) = (40*10^7)/(6371+(h(i)))^2;
v(i+1) = (v(i)+(a(i)*t(i)));
h(i+1) = (h(i))-(v(i)*(t(i))+1/2*a(i)*(dt)^2);
i=i+1
end
while 0 > h >= 100*10^3
t(i+1) = t(i)+dt;
a(i+1) = (40*10^7)/(6371+(h(i)))^2;
v(i+1) = (v(i)+(a(i)*t(i)));
h(i+1) = (h(i))-(v(i)*(t(i))+1/2*a(i)*(dt)^2);
i=i+1
end
When loop 1 is finished is their a way for loop 2 to start with the values that loop 1 had at the end if it's loop?

1 Comment

Stephen23
Stephen23 on 24 Feb 2018
Edited: Stephen23 on 24 Feb 2018
"When loop 1 is finished is their a way for loop 2 to start with the value"
Of course, MATLAB does not stop you from doing this. But you will need to fix the logical condition.

Sign in to comment.

 Accepted Answer

Stephen23
Stephen23 on 24 Feb 2018
Edited: Stephen23 on 24 Feb 2018
The syntax a<b<c is not supported by MATLAB, as all logical comparisons are binary operators, so you would need a<b & b<c. Note however that your logic will never be true anyway because 0 is not greater than 100*10^3, so there is no possible value of h which can be less than zero and greater than or equal to 100*10^3 at the same time.
Perhaps you meant this?:
while 0 < h & h <= 100*10^3
or perhaps using OR:
while 0 > h | h >= 100*10^3
If you explain in words what the required logic conditions are then we can help you to correct them.
I presume that you have read the while help and are also aware that non-scalar conditions require all elements to be true for while to run. For reasons of clarity it is recommended to make this explicit with all or any as required.

3 Comments

Tom Grundy
Tom Grundy on 24 Feb 2018
Edited: Tom Grundy on 24 Feb 2018
Apologies I did mess up with the logic and you are correct that it should be: while 0 < h & h <= 100*10^3, despite this change the program is still not working.
The task I’ve been set is to model a landing craft landing in my first loop I have set up equations for velocity, acceleration, time and height for when the landing craft is travelling in a place with no atmosphere, and the second while loop I’m going to do the same but for when the landing craft is travelling through an area with atmosphere, and stopping at the ground ( I haven’t changed the equation for acceleration yet to reflect this but I’m going to do that once I can model it such that the landing craft reaches the ground) , I hope this clarifies what I am attempting to do somewhat.
@Tom Grundy: I suspect that you might be wanting to check only the last calculated value, not all of the values at once as you are doing now, in which case you will need to use some indexing:
k = 1;
while h(k)>=100*10^3
...
k = k+1;
end
while h(k)<100*10^3 && h(k)>0
...
k = k+1;
end
Note that if the values of h are strictly decreasing then you do not need to test for h(k)>=100*10^3 in the second loop (the first loop will only finish when this is true, so there is no point in testing for it), therefore:
while h(k)>0
is probably enough for the second loop.
!!!!!! Thank you so much, I didn't really understand your question before sorry so I just tried to give you as much context :P, Thank you so much for your time and your patience <3.

Sign in to comment.

More Answers (0)

Categories

Find more on Automotive in Help Center and File Exchange

Tags

Asked:

on 24 Feb 2018

Edited:

on 24 Feb 2018

Community Treasure Hunt

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

Start Hunting!