# necessary imports
import numpy as np
import matplotlib.pyplot as plt
REPRODUCIBLE SETUP
Binet formula is the following, from here:
Let's define this function in python:
def binet(n):
phi = (1 + 5 ** 0.5) / 2
return ((phi**n) - (-1/phi)**n) / (5**0.5)
For the phi
value, I used this.
WHAT WORKS
Let's calculate binet(n)
for n=[0.1,0.2,0.3,0.4,0.5,...,4.9,5.0]
:
[binet(x/10) for x in range(1,51)]
Let's plot it:
# our results
plt.plot([n.real for n in binetn],[n.imag for n in binetn])
# classic fibonacci numbers
plt.scatter([1,1,3,5],[0,0,0,0],c='r')
Looks good, aggrees with this & our math knowledge.
WHAT DOESN'T WORK
Based on the above I was confident this is going to work as well:
binetn=[binet(x) for x in np.arange(0.1,5.1,0.1)]
It doesn't, however. binetn
becomes:
[nan,nan,nan,nan,nan,nan,nan,nan,nan,1.0,...,nan,nan,5.000000000000001]
Ie it is nan
except when binet(n)
is real.
It also gives a warning:
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in double_scalars
QUESTION
Why can I loop through a list of numbers generated by range()
& get complex results, while I cannot do the same with np.arange()
?