Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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?

share|improve this question
    
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. –  Rorschach Sep 15 '13 at 18:31
    
N /@ Union[ Round[#[[1, 2]], 10^-8] & /@ Table[FindRoot[sw[\[Pi]/2, \[Phi], -1] == 0, {\[Phi], x}], {x, 0, Pi, .1}]] –  belisarius Sep 15 '13 at 18:40
    
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. –  user9532 Sep 15 '13 at 19:03
1  
@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 –  Artes Sep 15 '13 at 19:26

1 Answer 1

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.

share|improve this answer
    
+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). –  Alexey Popkov Sep 16 '13 at 14:41
1  
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. –  Artes Sep 16 '13 at 15:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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