# How to select only the real, positive solutions to an equation (using NSolve)? [duplicate]

I'm trying to get Mathematica to solve a complicated equation for a particular variable using NSolve, with the conditions that the solutions must be real and $\geq 0$. Mathematica can solve the equation in general (including all numerical solutions, real and complex), however, when I impose the additional constraints, it doesn't seem to be able to compute the solutions in any reasonable amount of time. I have a feeling it might be something to do with the fact that for several of the input values there is more than one real, positive solution, but I might be mistaken.

Here is the code that I've written:

ClearAll["Global*"];

dk = 0.6;

m = 1;

f = 7;

lattsize = 15;

p[P_, [\Alpha]_, β_] := {P*Sin[α]*Cos[β],
P*Sin[α]*Sin[β], P*Cos[α]}

q[Q_, a_] := {Q*Sin[a], 0, Q*Cos[a]}

k[X_] := {0, 0, X}

X = Interpolation[Table[{i, i}, {i, 0, lattsize, dk}]];

ω[x_] := Sqrt[x.x + m^2]

Subscript[A, 1][x_, y_, z_,s_] := (1+1/(8*ω[x]^2))*ω[x](1+1/(8*ω[y]^2))*ω[y]+(1+1/(8*ω[z]^2))*ω[z]+(1+1/(8*ω[s]^2))*ω[s](*//FullSimplify*)

sol1 = Table[(tempsol=NSolve[Subscript[A, 1][p[P,α,β],q[Q,a],k[X[i]],k[X[i]]-p[P,α,β]-q[Q, a]]==f&&Q∈Reals&&Q>=0,Q];
If[tempsol=={},{{P,i,α,β,a},0},{{P,i,α,0,0},Q/.tempsol[[1]]}]),{P,0,5},{i, 0,lattsize},{α,0,1},{β,0,2},{a,0,1}]


Eventually I want to be able to interpolate this table in order to create a function. Is there a better (i.e. quicker and more efficient) way to extract the real, positive solutions other than the way that I've done it?

I'm a bit of a novice when it comes to Mathematica, so any help and/or suggestions would be very much appreciated

• @Artes I have read the linked answer and tried the following code 'Table[Select[Q /. NSolve[Subscript[A, 1][p[P, [Alpha], [Beta]], q[Q,a],k[X[i]],k[X[i]] - p[P, [Alpha], [Beta]] - q[Q, a]] == f , Q], Positive],{P, 0, 2}, {i, 0, 2}, {[Alpha], 0, 1}, {[Beta], 0, 2}, {a, 0, 1}]' However, it calculated there to be no solutions, which I know isn't true. – user35305 Jan 23 '17 at 18:09
• @Artes Scrap that, it seems to be working now, thanks :) .... How do I get Mathematica to select on the numerical value of the solution, for example 2.135 instead of {2.135}? If I tell Mathematica to pick out NSolve[Subscript[A, 1][p[P, [Alpha], [Beta]], q[Q,a],k[X[i]],k[X[i]] - p[P, [Alpha], [Beta]] - q[Q, a]] == f , Q][[1]] (for example) inside the Select function it doesn't work?! – user35305 Jan 23 '17 at 18:13
• See e.g. x /. {x -> 4}`, more detailed discussion Assign the results from a Solve to variable(s) – Artes Jan 23 '17 at 18:23
• @Artes Ok thanks, I'll take a look. – user35305 Jan 23 '17 at 18:37