Matlab’s Implementation of a While Loop with Multiple Conditions

The variables gammad1, etad, deltad, and beta1d are calculated based on the values of a, b, r1, r2, r3, beta2d, beta1dpoz, xs2, ys2, kruh1, kruh2, xA1, xA2, xAmat, xA, pyA1, yA1, elsyA, gamma, gammad, L, and q. The values of gammad1, etad, deltad, and beta1d are obtained by making necessary calculations using the given equations. If the value of beta1d is greater than beta1dpoz, it is set to beta1dpoz. The values of r1, xs2, ys2, kruh1, kruh2, xA1, xA2, xAmat, and xA are calculated using the given equations. The while loop is used to check whether the value of xA is real or not. If it is not real, the value of r1 is incremented by 0.1 and the values of xs2 and ys2 are recalculated.

Question:



Hello



I have a basic script to calculate a curve length. In first step I eliminate the complex number with a while
loop function
. In second step I try to received a curve length with one more specific parameter(second angle). But second “while” function do not work. I can not realize this problem. Can you help me with this problem?



if


true





% code



r1=0;



r2=110.5;



r3=212.5;



beta2d=35;



beta1dpoz=45;



format


long



xs2=r3*sind(beta2d); ys2=r1-r3*cosd(beta2d);



kruh1=xs2^2+ys2^2; kruh2=r1^2+r3^2-2*r1*r3*cosd(beta2d);



xA1=1/2*(xs2*(1+((r1^2-r2^2)/(xs2^2+ys2^2)))+ys2*(sqrt(((2*(r1^2+r2^2))/(xs2^2+ys2^2))-((r1^2-r2^2)/(xs2^2+ys2^2))^2-1))); xA2=1/2*(xs2*(1+((r1^2-r2^2)/(xs2^2+ys2^2)))-ys2*(sqrt(((2*(r1^2+r2^2))/(xs2^2+ys2^2))-((r1^2-r2^2)/(xs2^2+ys2^2))^2-1)));



xAmat=[xA1,xA2]; xA=max(xAmat);



while isreal(xA)==0



r1=r1+0.1;



xs2=r3*sind(beta2d);



ys2=r1-r3*cosd(beta2d);



kruh1=xs2^2+ys2^2;



kruh2=r1^2+r3^2-2*r1*r3*cosd(beta2d);



xA1=1/2*(xs2*(1+((r1^2-r2^2)/(xs2^2+ys2^2)))+ys2*(sqrt(((2*(r1^2+r2^2))/(xs2^2+ys2^2))-((r1^2-r2^2)/(xs2^2+ys2^2))^2-1)));



xA2=1/2*(xs2*(1+((r1^2-r2^2)/(xs2^2+ys2^2)))-ys2*(sqrt(((2*(r1^2+r2^2))/(xs2^2+ys2^2))-((r1^2-r2^2)/(xs2^2+ys2^2))^2-1)));



xAmat=[xA1,xA2];



xA=max(xAmat);



end



p=sqrt(2*r1^2+r3^2-2*r1*r3*(sind(beta2d)+cosd(beta2d)));



if r2>p



yA1=sqrt(r1^2-xA^2);



yA=yA1;



else



yA=-1*sqrt(r1^2-xA^2);



end



gamma=pi/2-atan(yA/xA);



gammad=gamma*180/pi;



L=2*pi*r1*gammad/360;



b=abs(abs(xs2)-abs(xA));



a=abs(abs(ys2)-abs(yA));



gammad1=gammad-90;



etad=atand(b/a);



deltad=abs(180-90-etad);



beta1d=deltad-gammad1;



if beta1d>=beta1dpoz



beta1d=beta1d



else



r1=r1





while


beta1d>=beta1dpoz



r1=r1+0.1



xs2=r3*sind(beta2d);



ys2=r1-r3*cosd(beta2d);



kruh1=xs2^2+ys2^2;



kruh2=r1^2+r3^2-2*r1*r3*cosd(beta2d);



xA1=1/2*(xs2*(1+((r1^2-r2^2)/(xs2^2+ys2^2)))+ys2*(sqrt(((2*(r1^2+r2^2))/(xs2^2+ys2^2))-((r1^2-r2^2)/(xs2^2+ys2^2))^2-1)));



xA2=1/2*(xs2*(1+((r1^2-r2^2)/(xs2^2+ys2^2)))-ys2*(sqrt(((2*(r1^2+r2^2))/(xs2^2+ys2^2))-((r1^2-r2^2)/(xs2^2+ys2^2))^2-1)));



xAmat=[xA1,xA2];



xA=max(xAmat);



p=sqrt(2*r1^2+r3^2-2*r1*r3*(sind(beta2d)+cosd(beta2d)));





if


r2>p



yA1=sqrt(r1^2-xA^2);



yA1=yA;





else



yA=-1*sqrt(r1^2-xA^2);





end



gamma=pi/2-atan(yA/xA);



gammad=gamma*180/pi;



L=2*pi*r1*gammad/360;



b=abs(abs(xs2)-abs(xA));



a=abs(abs(ys2)-abs(yA));



gammad1=gammad-90;



etad=atand(b/a);



deltad=180-90-etad;



beta1d=deltad-gammad1;





end



end



q=[r1 r2 r3 beta1d beta2d L];



end


Solution:



if


beta1d>=beta1dpoz



beta1d=beta1d



else



r1=r1





while


beta1d>=beta1dpoz









You’ve got the








while




clause in an








else




clause that ensures the condition is never








true




when that code section is reached.



Stated in another way, the test on the








while




is the same one as the T in the








if




so if it is








false




at that point the








while




test is also false and the








while




will never execute.

Frequently Asked Questions

Posted in Uncategorized