4

I am trying to understand the following code for image of logistic map,but I am stuck on the point where

ys = [ ]
rs = numpy.linspace(0, 4, 400) 

What does rs mean? What i in range() mean? I would really appreciate your help!

def f(x, r):
    """Discrete logistic equation with parameter r"""
    return r*x*(1-x)

if __name__ == '__main__':
    # initial condition for x
    ys = []
    rs = numpy.linspace(0, 4, 400)
    for r in rs:
        x = 0.1
        for i in range(500):
            x = f(x, r)

        for i in range(50):
            x = f(x, r)
            ys.append([r, x])

    ys = numpy.array(ys)
    pylab.plot(ys[:,0], ys[:,1], '.')
    pylab.show()
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Nasibabuba
  • 179
  • 1
  • 2
  • 6

3 Answers3

5
rs = numpy.linspace(0, 4, 400) 

creates a numpy array with 400 values equal-spaced between 0 and 4 (including the end-points).

Here are some smaller examples:

In [17]: import numpy as np

In [18]: np.linspace(0, 4, 5)
Out[18]: array([ 0.,  1.,  2.,  3.,  4.])

In [19]: np.linspace(0, 4, 10)
Out[19]: 
array([ 0.        ,  0.44444444,  0.88888889,  1.33333333,  1.77777778,
        2.22222222,  2.66666667,  3.11111111,  3.55555556,  4.        ])

for i in range(50) is explained in the Python tutorial (section 4.2 and 4.3).


Here are some comments to help explain the code.

import numpy as np
import matplotlib.pyplot as plt
import pylab
import numpy

def f(x, r):
    """Discrete logistic equation with parameter r"""
    return r*x*(1-x)

if __name__ == '__main__':
    # initial condition for x
    ys = []
    rs = numpy.linspace(0, 4, 400)

    # Loop through `rs`. `r` is assigned the values in `rs` one at a time. 
    for r in rs:
        x = 0.1
        # Repeat this loop 500 times. 
        # i is just a dummy variable since it is not used inside the for-loop.
        for i in range(500):
            # Evaluate f at (x, r). The return value is assigned to x.
            # x is then fed back into f(x, r). 
            # This makes x jump around 500 times according to the logistic equation.
            # r remains fixed.
            x = f(x, r)

        # Do this 50 times
        for i in range(50):
            # Again make the x jump around according to the logistic equation
            x = f(x, r)
            # Save the point (r, x) in the list ys
            ys.append([r, x])

    # ys is a list of lists.
    # You can also think of ys as a list of [r, x] point.
    # This converts the list of lists into a 2D numpy array.
    ys = numpy.array(ys)

    # ys[:,0] is a 1D array of r values
    # ys[:, 1] is a 1D array of x values
    # This draws a scatter plot of (r, x) points.
    pylab.plot(ys[:,0], ys[:,1], '.')
    pylab.show()
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
0
for i in range(500)

means that the contents of the loop will be evaluated 500 times (i will take on integer values from 0 to 499).

Neal Kruis
  • 2,055
  • 3
  • 26
  • 49
-1

There is also a syntactical error in the indexing of the array ys. It should be ys[:][0] and ys[:][1]. Python takes its array syntax from C and Java.

Glen Selle
  • 3,966
  • 4
  • 37
  • 59
Paula Thomas
  • 1,152
  • 1
  • 8
  • 13