0
$\begingroup$

I have the following function

K[theta_, phi_] := Module[{c1, c2, c3, Phi, PhiPr, x, y},
  {c1, c2, c3} = 
   1 - 3 Sin[theta]^2 Cos[phi - 2 Pi (# - 1)/3]^2 & /@ {1, 2, 3};
  Phi = Sqrt[((c2 + c3)^2 - c1^2)/(4 c2 c3)];
  PhiPr = ((c3 - c2)/c1) Phi;
  If[0 <= Phi^2 <= 1,
   {x, y} /. 
    Solve[Sin[Sqrt[3] x/2] == Phi && 
      Sin[3 y/2] == 
       PhiPr && (c2 + c3) Cos[Sqrt[3] x/2] == -c1 Cos[3 y/2] && -Pi <=
        x <= Pi && -Pi <= y <= Pi, {x, y}]
   , Null]
  ]

Sometimes it behaves itself and returns me either a Null (as desired) or an actual number. However sometimes it is a naughty function and returns something strange. For example if I compute K[0.1,0] it returns me {x$17889, y$17889}.

What is this output? I don't understand. Many thanks

$\endgroup$
  • 1
    $\begingroup$ When Solve cannot find a solution it returns {}. {x,y}/.{} is just {x,y}. But inside any module all the variable names are aliased with a $ and a claimed unique serial number. So that is what you are seeing, the aliased local variables from your Module. You could verify all this with a carefully positioned Print statement. $\endgroup$ – Bill Jan 2 '15 at 19:22
  • 1
    $\begingroup$ Related: mathematica.stackexchange.com/questions/40578/…. Perhaps there's a better candidate for a duplicate, but this is basically how Module works and is explained in the "Details" section of its documentation page. $\endgroup$ – Michael E2 Jan 2 '15 at 19:40
  • $\begingroup$ Ah.. I understand. Thank you $\endgroup$ – Tom Jan 2 '15 at 19:46
2
$\begingroup$

You can identify the problem by performing the steps in your function one at a time for theta = .1 and phi = 0..

{c1, c2, c3} = 1 - 3 Sin[0.1]^2 Cos[0 - 2 Pi (# - 1)/3]^2 & /@ {1, 2, 3}
{* {0.9701, 0.992525, 0.992525} *}

Phi = Sqrt[((c2 + c3)^2 - c1^2)/(4 c2 c3)]
{* 0.87245 *}

PhiPr = ((c3 - c2)/c1) Phi
{* 0. *}

Because 0 <= Phi^2 <= 1 is True, the Solve is executed:

{x, y} /. Solve[Sin[Sqrt[3] x/2] == Phi && Sin[3 y/2] == 
    PhiPr && (c2 + c3) Cos[Sqrt[3] x/2] == -c1 Cos[3 y/2] && -Pi <= x <= Pi && -Pi <= y <= Pi, 
    {x, y}]

returning the error message Solve::ratnz and {x, y} instead of an answer. Returning now to your original function, a Module, x and y are temporary variables, which have names beginning with $, which is what you are getting.

Addendum

Replace the If statement in the Module by

If[0 <= Phi^2 <= 1, Check[{x, y} /. Solve[Sin[Sqrt[3] x/2] == Phi && 
  Sin[3 y/2] == PhiPr && (c2 + c3) Cos[Sqrt[3] x/2] == -c1 Cos[3 y/2] && -Pi <= x <= Pi &&
  -Pi <= y <= Pi, {x, y}], Null], Null]]

to obtain a Null response is such cases.

$\endgroup$

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