[SciPy-User] Sequence not work

Warren Weckesser warren.weckesser at gmail.com
Wed Mar 27 08:12:48 EDT 2013


On 3/27/13, Robert Kern <robert.kern at gmail.com> wrote:
> On Wed, Mar 27, 2013 at 3:11 AM, El suisse <elsuizo37 at gmail.com> wrote:
>
>> Hi
>> I have to represent the following sequence:
>>
>> [image: z[2]=2]
>> [image: z[n+1]=2^{n-1/2}\sqrt{1-\sqrt{1-4^{1-n}z[n]^{2}}]  [image:
>> n=2,3,4....]
>>
>>
>> and my code is as follows:
>>
>>
>> #!/usr/bin/env python
>>
>>
>> import matplotlib.pyplot as plt
>>
>> import numpy as np
>>
>>
>> num_muestras = 100
>>
>> z = np.zeros(num_muestras)
>>
>>
>>
>> z[2] = 2.0  #En Python se cuenta desde el cero
>>
>> for n in range(2,num_muestras-1):
>>
>>
>>     z[n+1] = np.power(2,n-(1/2)) * (np.sqrt(1- np.sqrt(1-np.power(4,1-n)
>> *
>> np.power(z[n],2))) )
>>
>
> (1/2) should be 0.5 . (1/2) gives 0, not 0.5. The division of integers in
> Python 2.x defaults to returning an integer, not a floating point number
> (for a variety of reasons that you can Google for if you want an
> explanation).


Also, in numpy, an integer to a negative power gives 0:

In [21]: np.power(4, -3)
Out[21]: 0

so change np.power(4, 1-n) to np.power(4.0, 1-n), or even 4**(1-n),
since there is really no need to use a numpy function there.

And then only run your series out to about num_muestras = 30; you'll
see that it converges to pi, but then blows up.

Warren

>
> --
> Robert Kern
>



More information about the SciPy-User mailing list