Info

This question is closed. Reopen it to edit or answer.

Problem with iterating loop again if output is less than some fixed value

1 view (last 30 days)
Hi,
I am trying to calculate theta1, theta2 and r using following equations.
N=100;
for j=2:N
t(j)=t(j-1)+dt; %theta1, theta2 and r do not depend on time explicitly.
k=0.95;% input parameter
theta1(j)=theta1(j-1)+dt*k*(sin(theta2(j-1)-theta1(j-1))));
theta2(j)=theta2(j-1)+dt*k*(sin(theta1(j-1)-theta2(j-1))));
r(j)=Equation that depends on theta1 and theta2
At the middle of iteration if the value of r goes below some value say 0.5 then I want to use higher value of k(>0.95) and recalculate from the beginning using same equations given above.
I will be thankful for your help. Dharma

Answers (1)

Walter Roberson
Walter Roberson on 7 Mar 2018
k = 0.95;
N = 100;
while true
did_all = true;
for j = 2 : N
t(j)=t(j-1)+dt; %theta1, theta2 and r do not depend on time explicitly.
theta1(j)=theta1(j-1)+dt*k*(sin(theta2(j-1)-theta1(j-1))));
theta2(j)=theta2(j-1)+dt*k*(sin(theta1(j-1)-theta2(j-1))));
r(j)=Equation that depends on theta1 and theta2
if r(j) < 0.5;
did_all = false;
break;
end
end
if did_all; break; end
k = k + 0.05;
end
  8 Comments
Dharma
Dharma on 21 Mar 2018
Thank you Walter.
I am requesting suggestion for one more question.
This time, I am not starting over the 'j' loop with different value of k. When r (j) is in the interval lets say between 0 and 0.4 then I want to use higher value of k otherwise continue with lower value of k. Dharma
Dharma
Dharma on 21 Mar 2018
Let me be more specific. I am trying to put new higher value (but fixed) of k on such particular time intervals t (j) where value of r (j) is found in the range 0 to 0.4.

This question is closed.

Community Treasure Hunt

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

Start Hunting!