How to iterate this

조회 수: 4 (최근 30일)
Gianluca
Gianluca 2012년 11월 10일
Hi,
I need to iterate a series of equation for a problem. In particular I want to iterate them for 12 times. For the very first iteration I want to put two conditions (Lambda1dotZero=0 and Lambda=0); then from the second iteration onward Lambda1dotzero must be equal to the value of Lambda1dotZero of the previous iteration + a certain value DeltaOmega (which I know, and is constant). The same for Lambda, which from the second iteration onward, must be equal to that equation that you see in the loop (Lambda(m)=.....) + the value of Lambda of the previous iteration. Sorry but I am very new in Matlab, and this is the code that I wrote:
for m=1:1:12
while m==1;
Lambda1dotZero(m)=0;
Lambda(m)=0;
Lambda1dot(m)=Lambda2dot*t_days+Lambda1dotZero(m);
Lambda(m)=Lambda2dot*t_days^2/2+Lambda1dotZero(m)*t_days+Lambda(m);
end
while m>1;
Lambda1dotZero(m)=Lambda1dotZero(m-1)+DeltaOmega;
Lambda1dot(m)=Lambda2dot*t_days+Lambda1dotZero(m);
Lambda(m)=Lambda2dot*t_days^2/2+Lambda1dotZero(m)*t_days+Lambda(m-1);
end
end
When I run it, it doesn't give me errors but takes a long time without giving any results. Maybe I have created an infinite loop. I don't know. Many thanks to anyone who will help me.
Regards
Gian

채택된 답변

Walter Roberson
Walter Roberson 2012년 11월 10일
for m=1:1:12
if m==1
Lambda1dotZero(m)=0;
Lambda(m)=0;
Lambda1dot(m)=Lambda2dot*t_days+Lambda1dotZero(m);
Lambda(m)=Lambda2dot*t_days^2/2+Lambda1dotZero(m)*t_days+Lambda(m);
end
if m>1
Lambda1dotZero(m)=Lambda1dotZero(m-1)+DeltaOmega;
Lambda1dot(m)=Lambda2dot*t_days+Lambda1dotZero(m);
Lambda(m)=Lambda2dot*t_days^2/2+Lambda1dotZero(m)*t_days+Lambda(m-1);
end
end
Remember, a while loop executes its body until the condition is false and does not go on to the next section until then, but since m was not changing in the body, your while loop was infinite.
  댓글 수: 1
Gianluca
Gianluca 2012년 11월 10일
Thank you very much for your help man. The problem was about the motion of a satellite, and the results that Matlab gives me seem right. So the code works perfectly! Also now I understand how the while loop works ;)
Thanks again
Regards
Gian

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by