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 Kommentar

Stephen23
Stephen23 am 24 Feb. 2018
Bearbeitet: Stephen23 am 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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Stephen23
Stephen23 am 24 Feb. 2018
Bearbeitet: Stephen23 am 24 Feb. 2018

0 Stimmen

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 Kommentare

Tom Grundy
Tom Grundy am 24 Feb. 2018
Bearbeitet: Tom Grundy am 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.
Stephen23
Stephen23 am 24 Feb. 2018
Bearbeitet: Stephen23 am 24 Feb. 2018
@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.
Tom Grundy
Tom Grundy am 24 Feb. 2018
!!!!!! 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Automotive finden Sie in Hilfe-Center und File Exchange

Tags

Gefragt:

am 24 Feb. 2018

Bearbeitet:

am 24 Feb. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by