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
0 个评论
回答(1 个)
Walter Roberson
2018-3-7
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 个评论
Dharma
2018-3-10
Walter Roberson
2018-3-11
" 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.
Dharma
2018-3-13
Walter Roberson
2018-3-13
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
Dharma
2018-3-15
Walter Roberson
2018-3-15
"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
Dharma
2018-3-21
Dharma
2018-3-21
此问题已关闭。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!