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

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

回答(1 个)

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 个评论

Walter:
Thank you so much. After using your suggested code multiple times, now I have realized that this code uses increasing value of k unless r (j) becomes 0.4. For periodic function (lets say r (j) is equal to sine of theta1 and theta2), lower value of k still fulfills the condition i.e. r (j) >0.4 for some time interval. And of course, we need higher value of k at some points. Is it feasible to iterate r (j) with higher value of k sometimes (if r (j)<0.4) and then using previously used lower value of k in above code?
" Is it feasible to iterate r (j) with higher value of k sometimes (if r (j)<0.4) and then using previously used lower value of k in above code?"
No, you defined that k had to be increased if r(j) < 0.5, which contradicts using a lower value for k for r(j) > 0.4 over the range 0.4 to 0.5 . You cannot have a range over which k both needs to be increased but also needs to not be increased.
Walter: Thank you again.
I have one more question.I am trying to impose another condition. If r (j)>0.9 then I want to use lower value of k. I guess it would be k=k-0.05. Let me summarize: 1. If r (j)<0.4 then k=k+0.05 2. If r(j)>0.8 then k=k-0.05 Is it feasible?
k = 0.95;
N = 100;
while true
mama_bear = false;
papa_bear = false;
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.4;
mama_bear = true;
break;
end
if r(j) > 0.8
papa_bear = true;
break;
end
end
if mama_bear
k = k + 0.05; %it was too soft
elseif papa_bear
k = k - 0.05; %it was too hard
else
break; %it was just right
end
end
Walter,
Thank you so much for continuous assistance.
I still have one curiosity. the 'break statement' after first if condition should close 'for loop (j=2:N)' and another 'break statement' should also close the same 'for loop (j=2:N)'. Are they closing same 'for loop' independently? I am still in early phase of Matlab programming so I am not sure whether this is my reasonable question. My curiosity behind these is "for r (j)<0.4, k=k+0.05" and " for r (j)>0.8, k=k-0.05" are two independent conditions.
Dharma
"Are they closing same 'for loop' independently?"
Yes.
You could also code
mama_bear = r(j) < 0.4;
papa_bear = r(j) > 0.8;
if mama_bear || papa_bear; break; end
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
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.

此问题已关闭。

提问:

2018-3-7

关闭:

2021-8-20

Community Treasure Hunt

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

Start Hunting!

Translated by