2
$\begingroup$

I have the code:

theta = 0.1; phi = 0.1;
a = 1 - 3 Sin[theta]^2*Cos[phi]^2;
b = 1 - 3 Sin[theta]^2*Cos[phi - (2 Pi/3)]^2;
c = 1 - 3 Sin[theta]^2*Cos[phi - (4 Pi/3)]^2;
Solve[
  a*b*Sin[(Sqrt[3] x - 3 y)/2] == 
  a*c*Sin[(Sqrt[3] x + 3 y)/2] == 
  -b*c*Sin[Sqrt[3] x], 
  {x, y}]

which I left running for 10 minutes before aborting. However if I simply replace the constants with their numerical values, aka I replace the last line above by

Solve[
  0.965496 Sin[(Sqrt[3] x - 3 y)/2] == 
  0.960503 Sin[(Sqrt[3] x + 3 y)/2] == 
  -0.984803 Sin[Sqrt[3] x], 
  {x, y}]

The above runs straight away with no problems. I don't understand why it is doing this. Any pointers?

$\endgroup$

3 Answers 3

5
$\begingroup$

The problem is that Solve is having trouble with the mixture of exact and inexact numbers you are feeding it. If you make all the number exact or all inexact, it is a lot happier and quicker. I think it best to make all the numbers exact and apply N to get a numerical result after the solutions are found. Chop is needed to get rid of some numerical evaluation fuzz of the form u + 0. I v.

theta = 1/10; phi = 1/10;
a = 1 - 3 Sin[theta]^2*Cos[phi]^2;
b = 1 - 3 Sin[theta]^2*Cos[phi - (2 Pi/3)]^2;
c = 1 - 3 Sin[theta]^2*Cos[phi - (4 Pi/3)]^2;

Solve[
  a*b*Sin[(Sqrt[3] x - 3 y)/2] == 
  a*c*Sin[(Sqrt[3] x + 3 y)/2] == 
  -b*c*Sin[Sqrt[3] x], 
  {x, y}
] // N // Chop
{{x -> ConditionalExpression[7.2552 C[1], C[1] ∈ Integers], 
  y -> ConditionalExpression[-4.18879 C[2], C[2] ∈ Integers]}, 
 {x -> ConditionalExpression[7.2552 C[1], C[1] ∈ Integers], 
  y -> ConditionalExpression[-0.666667 (3.14159 + 6.28319 C[2]), C[2] ∈ Integers]},
...
 {x -> ConditionalExpression[1.1547 (-2.08165 + 6.28319 C[1]), C[1] ∈ Integers], 
  y -> ConditionalExpression[-0.666667 (-0.00462451 + 6.28319 C[2]), C[2] ∈ Integers]}}

It is likely you will want to get a set of solutions for specific values of C[1] and C[2], say C[1] = 1 and C[2] = 2. That can be done with

With[{m = 1, n = 2}, 
  Replace[
    Replace[sol, {C[1] -> m, C[2] -> n}, ∞], 
    ConditionalExpression[u_, _ ∈ Integers] -> u, ∞]]

which for the above solution returns

{{x -> 7.2552, y -> -8.37758}, {x -> 7.2552, y -> -10.472}, 
 {x -> 5.4414, y -> -7.33038}, {x -> 5.4414, y -> -9.42478}, 
 {x -> 9.069, y -> -7.33038}, {x -> 9.069, y -> -9.42478}, 
 {x -> 10.8828, y -> -8.37758}, {x -> 10.8828, y -> -10.472}, 
 {x -> 6.03128, y -> -6.28627}, {x -> 9.65888, y -> -8.38066}, 
 {x -> 8.47911, y -> -10.4689}, {x -> 4.85152, y -> -8.3745}}
$\endgroup$
4
  • $\begingroup$ When I copy and paste your first block of code into a fresh notebook it still takes forever to compute? However the '// N // Chop' was a very useful addition. I just include a Round[a*b,0.000001] etc for each prefactor and it works :) $\endgroup$
    – Tom
    Commented Jun 25, 2014 at 9:29
  • $\begingroup$ @ThomasJebbSturges. The important change was theta = 1/10; phi = 1/10;. With this change, solving your equations takes about 4 seconds on my system. $\endgroup$
    – m_goldberg
    Commented Jun 25, 2014 at 12:22
  • $\begingroup$ Hmm no, if i copy and paste your first block of code it still takes forever. I'm on version 8 which could be a reason. $\endgroup$
    – Tom
    Commented Jun 25, 2014 at 16:32
  • $\begingroup$ @m_goldberg you can simply write sol /. {C[1] -> 1, C[2] -> 2} $\endgroup$
    – eldo
    Commented Jul 4, 2014 at 14:33
1
$\begingroup$

Below is something that works in V8.0.4. Apparently something in Solve was improved in V9, even though the docs indicate it was last modified in V8.

theta = 0.1; phi = 0.1;
a = 1 - 3 Sin[theta]^2*Cos[phi]^2;
b = 1 - 3 Sin[theta]^2*Cos[phi - (2 Pi/3)]^2;
c = 1 - 3 Sin[theta]^2*Cos[phi - (4 Pi/3)]^2;
Chop @ N @ Solve[
  TrigExpand @ Rationalize[
    a*b*Sin[(Sqrt[3] x - 3 y)/2] == a*c*Sin[(Sqrt[3] x + 3 y)/2] == -b*c*Sin[Sqrt[3] x],
   0],
 {x, y}] // AbsoluteTiming

Solve::ifun: Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information. >>

(*
  {6.69512,
   {{x -> 0, y -> 0}, {x -> 0, y -> -6.28319}, ... {x -> 1.22392, y -> -6.2801}}}
*)

Comments:

The Solve::ifun warning indicates that Reduce might be a better function to use. Reduce will give all the solutions with C[1], C[2] parameters. Just replace Solve with Reduce, or use the option Solve[..., Method -> Reduce]. You can be confident that roots are not missed.

TrigExpand seems to break the terms down into easier-to-chew bites, probably because the arguments to the trigonometric functions become either (Sqrt[3] x)/2 or y/2, which might make it easier to isolate x or y. (It would by hand, which led me to try it.)

Rationalize[..., 0] turns the approximate real numbers into rational numbers, which have an exact representation. As @m_goldberg observes in an answer, Solve usually happier with exact numbers.

Unexplained

@m_goldberg's solution, to use exact numbers for theta and phi, does not work in the above. My only guess is that the coefficients a, b, and c have such complicated exact expressions that Solve has a difficult time with them.

I also cannot explain why it works with the rounded coefficients. It seems highly unlikely to me that decimal places have a role in trigonometric functions or numbers that internally are represented in binary.

$\endgroup$
3
  • $\begingroup$ Mr. Goldberg's solution works perfectly with 9.0. Please Explain your Unexplained. $\endgroup$
    – eldo
    Commented Jul 4, 2014 at 19:21
  • $\begingroup$ @eldo Did you try m_goldberg's theta = 1/10; phi = 1/10; in my code (= "the above")? $\endgroup$
    – Michael E2
    Commented Jul 4, 2014 at 19:53
  • $\begingroup$ Will try later - now it's soccer time in good old europe :) $\endgroup$
    – eldo
    Commented Jul 4, 2014 at 19:57
0
$\begingroup$

I figured it out (I think). It takes longer to compute depending on the number of decimal points. So I just used Round[a b,0.000001], etc. to keep the digital places down.

$\endgroup$

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.