Question: How to distinguish between real and complex solutions in solve?

solve_V_alfa.mws

 

I'm trying to solve two simultaneous equations for velocity, V, and angle of inclination, alfa, given the range, ranj and maximum height, H.  The solve part works fine, but gives me several unwanted complex solutions.  I was trying to put the solutions in a list so that I could access them.  The text below is rather verbose, but I've left comments in as a possible educational aid for people understanding expression sequences, and sets. 

restart:
#interface(imaginaryunit, errorbreak=0, errorcursor=true, #labelling=false):
#with(SolveTools);

# Investigating sets and expression sequences
#S:={q, w,e}:
S:=7,2,5:
whattype(S);
L4:=convert(S,list): #Doesn't work for expression sequences
L4:=[S]; # this converts the expression sequence to a list, L4
L4[3]; # outputs 3rd element of L4 which is 5
# End of investigation

print(`Next part - solving two equations: Ranj and height H given`);
print(`To find angle of inclination, alfa, and velocity, V`);
g:=9.81:Ranj:=80:H:=15:
L:=solve({4*V^2*cos(alfa)^2-g*Ranj^2/(Ranj*tan(alfa)-2*H), Ranj*g-V*sin(2*alfa)},{V, alfa}): #::real}):
#L:=solve([4*V^2*cos(alfa)^2-g*Ranj^2/(Ranj*tan(alfa)-2*H), #Ranj*g-V*sin(2*alfa)],{V, alfa}): #::real}):
# ::real, alfa::real:
printf("L is of type %a", whattype(L));
#whattype(L);
#L2:=convert(L,list); #gives error message
L2:=[L]:
L2;
print(`Next: To distinguish between real and complex solutions`);
print(`Output in if statement is not working as expected.`);
#nops([L]);
for i from 1 to nops([L]) do
if type(L[i],complex(float)) then
#if type(L[i],nonreal) then
printf("Complex solution for i=%d\n",i);
else
printf("else part of if: i=%d\n",i);
print( L[i]);
end if;
end do; #for

Download solve_V_alfa.txt

 

Please Wait...