0
$\begingroup$

If I define a function using

alpha[y_, d_] := 2 ArcCos[1 - (2 y)/d]

Then if $y$ and $d$ are equal, the result is $2\pi$ :

alpha[x, x]

and

alpha[4, 4]

are both evaluated as

$2 \pi$

But when I use this in a table with:

Table[alpha[y1, d1], {d1, 0.1, 5, 0.1}, {y1, 0, d1, 0.1}]

Some imaginary parts (although very small) for example $6.28319 - 5.96046*10^-8 i$ appear. Am I doing something wrong? It seems like something basic that I am doing wrong because of not understanding a concept in MMA.

$\endgroup$
9
  • 1
    $\begingroup$ Prolly just your machine's floating point arithmetic. On my box, I'm not getting tiny imaginary parts at all (10.4 on Xubuntu Trusty). Note that the evaluation of arccosine is ill-conditioned near $\pm 1$, so tiny perturbations in the argument can cause not very small perturbations in the output. $\endgroup$ Apr 12, 2016 at 17:06
  • 1
    $\begingroup$ Basically since your numbers in Table are approximate your problem appears to be trivial. Nevertheless if there is a certain problem at all, it was raised in his question How to eliminate the zero real part of a purely imaginary number?. The issue is version dependent. $\endgroup$
    – Artes
    Apr 12, 2016 at 17:09
  • $\begingroup$ @J.M. I'm using 10.3 on Windows 8. Good point about the ill-conditioned behavior. $\endgroup$
    – MathX
    Apr 12, 2016 at 17:13
  • 2
    $\begingroup$ It's not Table[] that's doing the approximation; it's your use of 0.1in the iterator. Try replacing all those with 1/10, and apply N[] afterwards. $\endgroup$ Apr 12, 2016 at 17:19
  • 1
    $\begingroup$ In general, inexact numbers "contaminate" every calculation in which they are introduced; the result's precision will be the same as that of the least precise number. In your case, the machine precision 0.1 resulted in everything being done in machine precision. $\endgroup$ Apr 12, 2016 at 18:05

1 Answer 1

1
$\begingroup$

Many thanks to @J.M. and @Artes I found out that the iterator 0.1 in the Table function was making this artifact, which is also version dependent.

@J. M.'s solution worked perfectly:

 Table[N[alpha[y1, d1]], {d1, 1/10, 5, 1/10}, {y1, 0, d1, 1/10}]
$\endgroup$
3
  • 1
    $\begingroup$ Specifying the precision of your iterator should also prevent the imaginary part from showing up: Table[alpha[y1, d1], {d1, 0.1`6, 5, 0.1`6}, {y1, 0, d1, 0.1`6}] $\endgroup$
    – RunnyKine
    Apr 13, 2016 at 5:16
  • $\begingroup$ @RunnyKine, thanks. If I want to use this, I guess I have to set the precision of everything else to the same order, right? $\endgroup$
    – MathX
    Apr 13, 2016 at 21:28
  • $\begingroup$ If you're using a function as in your question, then you don't need to. Setting the precision of the iterators like I did will just instruct Mathematica to carry out the calculation to that precision. $\endgroup$
    – RunnyKine
    Apr 13, 2016 at 21:38

Your Answer

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

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