7
$\begingroup$

I defined the following function:

sw[ θ_, ϕ_, h_] =  1/2 Sin[2 (ϕ - θ)] + h Sin[ϕ]

Based upon whether I use Solve or NSolve, I get different answers:

Solve[ sw[ π/2, ϕ, -1] == 0 && 0 <= ϕ <= π, ϕ]

I get different answers from:

NSolve[ sw[ π/2, ϕ, -1] == 0 && 0 <= ϕ <= π, ϕ]

The answer using Solve is complete and correct. However, with NSolve it only gives you a partial answer ϕ == 0 but not ϕ == π.

Does anyone have any idea why Mathematica 9 is doing this?
How do I get the same answer from both the methods?

$\endgroup$
5
  • $\begingroup$ In my opinion N* methods use some predefined approximations,i.e. matching it with some predefined patterns which makes it fast.It might not be correct always and M gives this warning at times. $\endgroup$ Commented Sep 15, 2013 at 18:31
  • $\begingroup$ N /@ Union[ Round[#[[1, 2]], 10^-8] & /@ Table[FindRoot[sw[\[Pi]/2, \[Phi], -1] == 0, {\[Phi], x}], {x, 0, Pi, .1}]] $\endgroup$ Commented Sep 15, 2013 at 18:40
  • $\begingroup$ Thanks for all the answers. I now have a consistent set of answers. Interestingly, the issue was occuring only when h = -1. In all other cases, both Solve and NSolve seemed to give the same answers. $\endgroup$
    – user9532
    Commented Sep 15, 2013 at 19:03
  • 1
    $\begingroup$ @user9532 Welcome to 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$
    – Artes
    Commented Sep 15, 2013 at 19:26
  • $\begingroup$ This seems fixed in Mathematica 11.2. I got {{\[Phi] -> 0.}, {\[Phi] -> 3.14159265358979}} for NSolve. $\endgroup$
    – user58955
    Commented Oct 9, 2017 at 1:25

1 Answer 1

9
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ +1. But strictly speaking, Solve[expr,vars] also assumes by default that quantities appearing algebraically in inequalities are real, while all other quantities are complex (according to the Documentation). $\endgroup$ Commented Sep 16, 2013 at 14:41
  • 1
    $\begingroup$ I agree, but I mean that Solve relies on different algorithms. To expose my argument once more: in NSolve[ sw[ π/2, ϕ, -1] == 0 && 0 <= ϕ <= π, ϕ] it is assumed that ϕ is a real variable. But taking 0 <= Re[ϕ] <= π or specifying domain Complexes it is assumed that ϕ is a complex variable, so that behind NSolve there work different algorithms which allow finding all solutions. $\endgroup$
    – Artes
    Commented Sep 16, 2013 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.