From: Stephen on
My code takes an initial angle ranging between 0 and 2pi radians. If the angle is between 0 and 0.5pi, I perform while loop A; if the angle is between 0.5pi and pi, I perform while loop B, and so on. The reason I have to do this is because I must manipulate the final output angle because asin only outputs the reference angle, and I need the actual angle. The issue I am having is that the code seems to hop over certain if and while loops and picks which one it wants to use. For instance, if I move the if statement for an angle between 1.5pi and 2pi to the top of the code, I get a different answer than I would if it were at the bottom of the code without changing anything else, obviously including inputs. I'm seriously stumped on this one and growing more impatient... I would include the code, but it has some equations that look even more massive than they really are in this small of
margin. I'm really hoping someone has an idea on what may be happening here. Thanks for any help that can be provided.
From: Yi Cao on
"Stephen " <hphilli5(a)utk.edu> wrote in message <hr3fp4$b61$1(a)fred.mathworks.com>...
> My code takes an initial angle ranging between 0 and 2pi radians. If the angle is between 0 and 0.5pi, I perform while loop A; if the angle is between 0.5pi and pi, I perform while loop B, and so on. The reason I have to do this is because I must manipulate the final output angle because asin only outputs the reference angle, and I need the actual angle. The issue I am having is that the code seems to hop over certain if and while loops and picks which one it wants to use. For instance, if I move the if statement for an angle between 1.5pi and 2pi to the top of the code, I get a different answer than I would if it were at the bottom of the code without changing anything else, obviously including inputs. I'm seriously stumped on this one and growing more impatient... I would include the code, but it has some equations that look even more massive than they really are in this small of
> margin. I'm really hoping someone has an idea on what may be happening here. Thanks for any help that can be provided.

What did you exactly put in the code for these if conditions? Without looking at your actual code, it is difficult to provide help.

Yi
From: Steven Lord on

"Stephen " <hphilli5(a)utk.edu> wrote in message
news:hr3fp4$b61$1(a)fred.mathworks.com...
> My code takes an initial angle ranging between 0 and 2pi radians. If the
> angle is between 0 and 0.5pi, I perform while loop A; if the angle is
> between 0.5pi and pi, I perform while loop B, and so on. The reason I have
> to do this is because I must manipulate the final output angle because
> asin only outputs the reference angle, and I need the actual angle. The
> issue I am having is that the code seems to hop over certain if and while
> loops and picks which one it wants to use. For instance, if I move the if
> statement for an angle between 1.5pi and 2pi to the top of the code, I get
> a different answer than I would if it were at the bottom of the code
> without changing anything else, obviously including inputs. I'm seriously
> stumped on this one and growing more impatient... I would include the
> code, but it has some equations that look even more massive than they
> really are in this small of margin. I'm really hoping someone has an idea
> on what may be happening here. Thanks for any help that can be provided.

Post just the "structural" code -- replace any of those really long
assignment statements/expressions like:

y = myReallLongFunctionCallAnd_OrExpression(t, y, theta, q, kitchenSink,
etc)

with:

y = ... % do stuff

My _suspicion_ is that you're doing something like this in your IF
statements or WHILE loops:

if 0 < theta < 0.5*pi
% do stuff
....

or that you're using isolated IF statements and changing theta in an earlier
IF that makes it satisfy the condition for a later IF that it otherwise
would not.

if theta < 0
theta = -theta;
end
if theta > pi
theta = theta/2;
end

rather than:

if theta < 0
theta = -theta;
elseif theta > pi
theta = theta/2;
end

If you used the former code with theta = -2*pi, theta would end up
containing pi afterwards; if you used the latter code with theta = -2*pi,
theta would end up being 2*pi.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: Stephen on
"Steven Lord" <slord(a)mathworks.com> wrote in message <hr45t1$96l$1(a)fred.mathworks.com>...
>
> "Stephen " <hphilli5(a)utk.edu> wrote in message
> news:hr3fp4$b61$1(a)fred.mathworks.com...
> > My code takes an initial angle ranging between 0 and 2pi radians. If the
> > angle is between 0 and 0.5pi, I perform while loop A; if the angle is
> > between 0.5pi and pi, I perform while loop B, and so on. The reason I have
> > to do this is because I must manipulate the final output angle because
> > asin only outputs the reference angle, and I need the actual angle. The
> > issue I am having is that the code seems to hop over certain if and while
> > loops and picks which one it wants to use. For instance, if I move the if
> > statement for an angle between 1.5pi and 2pi to the top of the code, I get
> > a different answer than I would if it were at the bottom of the code
> > without changing anything else, obviously including inputs. I'm seriously
> > stumped on this one and growing more impatient... I would include the
> > code, but it has some equations that look even more massive than they
> > really are in this small of margin. I'm really hoping someone has an idea
> > on what may be happening here. Thanks for any help that can be provided.
>
> Post just the "structural" code -- replace any of those really long
> assignment statements/expressions like:
>
> y = myReallLongFunctionCallAnd_OrExpression(t, y, theta, q, kitchenSink,
> etc)
>
> with:
>
> y = ... % do stuff
>
> My _suspicion_ is that you're doing something like this in your IF
> statements or WHILE loops:
>
> if 0 < theta < 0.5*pi
> % do stuff
> ...
>
> or that you're using isolated IF statements and changing theta in an earlier
> IF that makes it satisfy the condition for a later IF that it otherwise
> would not.
>
> if theta < 0
> theta = -theta;
> end
> if theta > pi
> theta = theta/2;
> end
>
> rather than:
>
> if theta < 0
> theta = -theta;
> elseif theta > pi
> theta = theta/2;
> end
>
> If you used the former code with theta = -2*pi, theta would end up
> containing pi afterwards; if you used the latter code with theta = -2*pi,
> theta would end up being 2*pi.
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

-----------------------------------------------------------------------------------------------
Ok, here is a simplified version of the code:

clear all
clc
format long g
N=13713.44;
Vn=8000;
Gn=-10;
Hn=45;
A=125000;
Rearth=6378000;
LA=0;
Gr=Gn;
diff=1;
if 0<=Hn<=90
while diff>0.000001
f=f(Gr) % f=0 (Newton-Raphson Method)
df=derivative of f with respect to Gr
Gr2=Gr-f/df;
diff=abs(Gr2-Gr);
Gr=Gr2;
Hr=asind((Vn*cosd(Gn)*sind(Hn))/(((Vn*sind(Gn))/sind(Gr))
*cosd(Gr)));
Vr=Vn*sind(Gn)/sind(Gr);
end
end
disp(['Gr = ' num2str(Gr), ' degrees'])
disp(['Gn = ' num2str(Gn), ' degrees'])
disp(['Vr = ' num2str(Vr), ' m/s'])
disp(['Vn = ' num2str(Vn), ' m/s'])
disp(['Hr = ' num2str(Hr), ' degrees'])
disp(['Hn = ' num2str(Hn), ' degrees'])

The actual code contains 3 more if statements to cover all four quadrants and each of the while loops associated with those if statements has a different function, f.

The problem is that if Hn is changed to anything outside the range defined by the if statement, say 120, the code still runs the while loop with that Hn. Shouldn't it immediately finish the program when that if statement checks to see if the input Hn is between the range specified and it is not?

I am really stumped on this one. I have even tried imbedding all of the if statements within one while loop rather the other way around and this issue still occurs.

I really appreciate all the help thus far and any future insight into fixing this problem
From: TideMan on
On Apr 27, 10:55 am, "Stephen " <hphil...(a)utk.edu> wrote:
> "Steven Lord" <sl...(a)mathworks.com> wrote in message <hr45t1$96...(a)fred.mathworks.com>...
>
> > "Stephen " <hphil...(a)utk.edu> wrote in message
> >news:hr3fp4$b61$1(a)fred.mathworks.com...
> > > My code takes an initial angle ranging between 0 and 2pi radians. If the
> > > angle is between 0 and 0.5pi, I perform while loop A; if the angle is
> > > between 0.5pi and pi, I perform while loop B, and so on. The reason I have
> > > to do this is because I must manipulate the final output angle because
> > > asin only outputs the reference angle, and I need the actual angle. The
> > > issue I am having is that the code seems to hop over certain if  and while
> > > loops and picks which one it wants to use. For instance, if I move the if
> > > statement for an angle between 1.5pi and 2pi to the top of the code, I get
> > > a different answer than I would if it were at the bottom of the code
> > > without changing anything else, obviously including inputs. I'm seriously
> > > stumped on this one and growing more impatient... I would include the
> > > code, but it has some equations that look even more massive than they
> > > really are in this small of margin. I'm really hoping someone has an idea
> > > on what may be happening here. Thanks for any help that can be provided.
>
> > Post just the "structural" code -- replace any of those really long
> > assignment statements/expressions like:
>
> > y = myReallLongFunctionCallAnd_OrExpression(t, y, theta, q, kitchenSink,
> > etc)
>
> > with:
>
> > y = ... % do stuff
>
> > My _suspicion_ is that you're doing something like this in your IF
> > statements or WHILE loops:
>
> > if 0 < theta < 0.5*pi
> > % do stuff
> > ...
>
> > or that you're using isolated IF statements and changing theta in an earlier
> > IF that makes it satisfy the condition for a later IF that it otherwise
> > would not.
>
> > if theta < 0
> >     theta = -theta;
> > end
> > if theta > pi
> >     theta = theta/2;
> > end
>
> > rather than:
>
> > if theta < 0
> >     theta = -theta;
> > elseif theta > pi
> >     theta = theta/2;
> > end
>
> > If you used the former code with theta = -2*pi, theta would end up
> > containing pi afterwards; if you used the latter code with theta = -2*pi,
> > theta would end up being 2*pi.
>
> > --
> > Steve Lord
> > sl...(a)mathworks.com
> > comp.soft-sys.matlab (CSSM) FAQ:http://matlabwiki.mathworks.com/MATLAB_FAQ
>
> -----------------------------------------------------------------------------------------------
> Ok, here is a simplified version of the code:
>
> clear all
> clc
> format long g
> N=13713.44;
> Vn=8000;
> Gn=-10;
> Hn=45;
> A=125000;
> Rearth=6378000;
> LA=0;
> Gr=Gn;  
> diff=1;
> if 0<=Hn<=90
>     while diff>0.000001  
>         f=f(Gr)   % f=0 (Newton-Raphson Method)
>         df=derivative of f with respect to Gr
>         Gr2=Gr-f/df;  
>         diff=abs(Gr2-Gr);  
>         Gr=Gr2;  
>         Hr=asind((Vn*cosd(Gn)*sind(Hn))/(((Vn*sind(Gn))/sind(Gr))
>                      *cosd(Gr)));
>         Vr=Vn*sind(Gn)/sind(Gr);
>     end
> end
> disp(['Gr = ' num2str(Gr), ' degrees'])
> disp(['Gn = ' num2str(Gn), ' degrees'])
> disp(['Vr = ' num2str(Vr), ' m/s'])
> disp(['Vn = ' num2str(Vn), ' m/s'])
> disp(['Hr = ' num2str(Hr), ' degrees'])
> disp(['Hn = ' num2str(Hn), ' degrees'])
>
> The actual code contains 3 more if statements to cover all four quadrants and each of the while loops associated with those if statements has a different function, f.
>
> The problem is that if Hn is changed to anything outside the range defined by the if statement, say 120, the code still runs the while loop with that Hn. Shouldn't it immediately finish the program when that if statement checks to see if the input Hn is between the range specified and it is not?
>
> I am really stumped on this one. I have even tried imbedding all of the if statements within one while loop rather the other way around and this issue still occurs.
>
> I really appreciate all the help thus far and any future insight into fixing this problem

What you've given us is totally irrelevant.

I suspect you have done what Steven surmised and you have further if
statements below this snippet of code.
You need to put them all within one if statement, separated by elseifs
So it would look like this:
if 0<=Hn<=90
do stuff
elseif 90 <= Hn < 180
do different stuff
elseif 180 <= Hn < 270
do stuff
elseif 2700 <= Hn < 360
do stuff
else
error('Hn must lie between 0 and 360')
end