How to define multiple while loop conditions?

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

 Akzeptierte Antwort

dpb
dpb am 26 Okt. 2013

0 Stimmen

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.

6 Kommentare

Roman
Roman am 26 Okt. 2013
Hello dpb thank you for your help and time As I understand the philosophy of that part of code:
First step: With the first while function find the value of r1 to calculate first xA which is real number and calculate value for beta1d with values r1 and xA.
Second step: If first calculated beta1d is higher or equals to required value (beta1dpoz) use this original value (beta1d) else - beta1d>=beta1dpoz is not true, calculate entire script below the "while". In first step add 0.1 to original r1(first usefull r1) and calculate until beta1d will be higher or equals to beta1dpoz.
Am I understand well that philosophy ? Do you have any other suggestions?
dpb
dpb am 26 Okt. 2013
I don't know anything about any "philosophy"... :)
You've described what the code does correctly, the problem is that there's obviously a flaw in the logic. The while inside the else clause of the if can't ever execute because as stated above if the else is true the while is false because as written they're the same test, identically.
Now, what is really intended is another question -- we don't know that from what you've posted. Describe in words precisely what it is you're trying to get as a result and folks can probably help in the implementation logic.
Roman
Roman am 26 Okt. 2013
Bearbeitet: Roman am 26 Okt. 2013
Ok thank you for your willingness :) So...the basic problem is described here (I created a basic description): curve lenght
It is a analytic solution for the curve length and the script follows this solution. Part while isreal(xA)==0 is for avoid unreal coordinates. Other parts follow this algorithm exactly. beta1d is just calculated for verification. This solution is fine, if you know the r1 r2 r3 and beta2d, but I have no effect to beta1d.
But in many cases known parameters are r2 r3 beta1d beta2d so I want to calculate r1. That's my problem.
I tried to make function in another m-file to avoid two while funcions, but I can not access global parameters for a function :(
Again. Thank you for your time :)
dpb
dpb am 26 Okt. 2013
Bearbeitet: dpb am 26 Okt. 2013
OK, now I'm really confused--why do you need any while loops at all?
Why isn't it just solving for the intersection point to get the angle and then the length is the ratio to the total perimeter??? The only logic test I see is to select the proper solution of the root of the quadratic.
Roman
Roman am 26 Okt. 2013
Bearbeitet: Roman am 26 Okt. 2013
Because if I don't know r1 but know beta1d I it is possible to approximate r1.
Finally I relize it :) There was a logic problem as you said. Again...Thank you for your help, patience and willingness. :)
if beta1d>=beta1dpoz
beta1d=beta1d
else
for r1=r1:0.1:inf
.
.
.
etad=atand(b/a);
deltad=180-90-etad;
beta1d=deltad-gammad1;
while beta1d>=beta1dpoz
return
end
end
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Graphics Performance finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 26 Okt. 2013

Kommentiert:

am 26 Okt. 2013

Community Treasure Hunt

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

Start Hunting!

Translated by