Although the both functions seem to be quite similar, nontheless one shouldn't expect that Solve
and NSolve
always yield the (same) equivalent solutions. The first returns symbolic (exact) solutions, while the latter gives numerical approximations to the solutions, behind the scenes they use different algorithms and different options are accessible, therefore we could expect appearence of various subtle issues.
The both functions basically assume that variables are complex, however in inequalities when algebraic variables are involved they are assumed to be real, let's take a look in the help page of NSolve
:
NSolve[ expr, vars] assumes by default that quantities appearing algebraically
in inequalities are real, while all other quantities are complex.
Thus it is ensured that NSolve[ sw[ π/2, ϕ, -1] == 0 && 0 <= ϕ <= π, ϕ]
does the same as
NSolve[ sw[ π/2, ϕ, -1] == 0 && 0 <= ϕ <= π, ϕ, Reals]
. One might suspect that some solution are omitted because they lie on the boundary of the region 0 <= ϕ <= π
, nonetheless this input
NSolve[ sw[ π/2, ϕ, -1] == 0 && 0 <= ϕ <= 4, ϕ]
{{ϕ -> 0.}}
either provides an incomplete set of solutions. This is of course an instance of a bug in NSolve
.
On the other hand we can find a different route to get the full set of (numerical) solutions. Namely we are going to make NSolve
select different algorithms imposing the complex domain. We can provide here two methods.
1. One method imposes a restriction on the real part of ϕ
:
NSolve[ sw[ π/2, ϕ, -1] == 0 && 0 <= Re[ϕ] <= π, ϕ]
{{ ϕ -> 0.}, { ϕ -> 3.14159}, { ϕ -> 3.14159}, { ϕ -> 3.14159}}
2. Another and even simpler approach explicitly includes the Complexes
domain:
NSolve[ sw[ π/2, ϕ, -1] == 0 && 0 <= ϕ <= π, ϕ, Complexes]
{{ ϕ -> 0.}, { ϕ -> 3.14159}, { ϕ -> 3.14159}, { ϕ -> 3.14159}}
Now we can see that Solve
and NSolve
provided equivalent solutions.
N*
methods use some predefined approximations,i.e. matching it with some predefined patterns which makes it fast.It might not be correct always andM
gives this warning at times. $\endgroup$N /@ Union[ Round[#[[1, 2]], 10^-8] & /@ Table[FindRoot[sw[\[Pi]/2, \[Phi], -1] == 0, {\[Phi], x}], {x, 0, Pi, .1}]]
$\endgroup$mathematica.SE
. If you want to interact with this site better, please register your account, so that when you collect votes you'll be able to do more on the site (adding comments, edditing posts, voting, etc.). See privileges $\endgroup${{\[Phi] -> 0.}, {\[Phi] -> 3.14159265358979}}
for NSolve. $\endgroup$