0

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.

MathX
  • 1,614
  • 11
  • 17
  • 1
    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. – J. M.'s missing motivation Apr 12 '16 at 17:06
  • 1
    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. – Artes Apr 12 '16 at 17:09
  • @J.M. I'm using 10.3 on Windows 8. Good point about the ill-conditioned behavior. – MathX Apr 12 '16 at 17:13
  • @Artes Getting rid of this is not the point, I can eliminate it easily. But I had this idea that Table can be used to do just the same thing we are doing for one expression, more than once. I didn't know it has a built-in approximation functionality. I want to make sure I understand how things work so as to not repeat the same mistakes. – MathX Apr 12 '16 at 17:16
  • 2
    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. – J. M.'s missing motivation Apr 12 '16 at 17:19
  • @J.M. Oh! Now that's the answer I was looking for. Thanks a lot! It makes sense now. – MathX Apr 12 '16 at 17:21
  • Can you answer your own question now, then? :) – J. M.'s missing motivation Apr 12 '16 at 17:27
  • I think from now on I'll always use exact iterators, just in case. Interesting that I didn't see this in any introductory documents. – MathX Apr 12 '16 at 17:45
  • 1
    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. – J. M.'s missing motivation Apr 12 '16 at 18:05

1 Answers1

1

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}]
MathX
  • 1,614
  • 11
  • 17
  • 1
    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}] – RunnyKine Apr 13 '16 at 05:16
  • @RunnyKine, thanks. If I want to use this, I guess I have to set the precision of everything else to the same order, right? – MathX Apr 13 '16 at 21:28
  • 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. – RunnyKine Apr 13 '16 at 21:38