How to stop a for loop when it produces complex results

I'm using a for loop to alter 2 components of my code a velocity variable (v0) and an angle variable (lambda1). These numbers can combine to produce either real numbers or complex numbers based on their specific values. When they combine to produce a complex number a few lines down the code stops because it involves a function (phi1) that must be real. I'd like to code in a way for it to first check if the combo produces a complex root and if it does, skip back to the loop to change one of the values so that the program doesnt break. Is this possible?
v0 = ; %Du/Tu
phi0 = 0 ;
lambda1 = 30 ;
r1 = sqrt( D^2 + Rsoi^2 - 2*D*Rsoi*cos(lambda1*pi/180) ) ;
Energy0 = v0^2/2 - mu_e/R0 ;
h = R0*v0 ;
v1 = sqrt( 2 * (Energy0 + mu_e/r1)) ;
phi1 = acosd( h / (r1*v1)) ;

1 Comment

I assume it would be more clear, if you show the code of the loop.

Sign in to comment.

Answers (1)

isreal()

2 Comments

does this just check that its real or does itgo back to the for loop if it isnt?
@Grant: It is trivial to get the answer by your own: "help isreal".

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 3 Nov 2011

Community Treasure Hunt

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

Start Hunting!