Facebook Connect Login Box

Connect with facebook

Hi , login or create a new account below

Login

If you already have an account with this website login with your existing user name and password to enable Facebook Connect

Forgot your password? Forgot you username?
Register

Alternativley to create a new account using details from your Facebook profile enter your desired user name and password below

Powered by myApi

Registration and login on this web site has been made faster and easier by myApi, the Facebook Connect Joomla bridge

There is no need to worry, this website will never be able gain access to your account, or personal data you do not explicitly give it permission to use

Creative Commons License
Unless specified otherwise
 

Logistic Map in Python

Whenever I have an assignment to do, I try to do it in a procedure, or a style that I am not familiar with and also that I am eager to learn. This was once typesetting with LaTeX2e, now I am messing around with Python, so instead of realizing Logistic Map in Matlab, I tried to accomplish the task that was given in Python, though I had to put more effort in doing so. 

For the more technically inclined, I am using Python 2.6.4, with Stani's Python Editor, on Ubuntu.  I don't think that would make a lot difference, but for the sake of precision, I wanted to state it. In my opinion, those are a good choice to start, and also I will try Eclipse with Pydev,  since it is an IDE I am more accustomed with.

The Logistic map is a  second order mapping, that is also called a logistics equation in which I will be analyzing as a discrete mapping henceforth. So in our case, 

 f:x\in(0,1)\subset\mathbb{R^1}\mapsto x \in(0,1) \subset \mathbb{R^1}

And we are going to use our logistic map such that the output generated by an nth of a series, will be introduced to another logistic function, so (n+1)th element of the series is obtained. In a more compact way:

 x[n+1] = f(x[n],r) = rx[n](1-x[n])  

that relation causes a simple bevahior when x values evolve with time if r<3. Simply, x values converge into a finite, real value when this relation holds. If r=3, the system undergoes a bifurcation and two different equillibrium points are generated. The behaviour of the system gets more and more complex with r getting larger. At some point, the system behaves chaotically, in other words, the values diverge form the values when x is introduced with an infinitesimally small perturbation. 

I coded in a function oriented fashion, though Python is a language that is designed for an object oriented coding style. The following can also be witthen in C pretty easily, and the runtime would be much less. Also, those are coded in a non-optimized way, I guess I like using Python a bit like Matlab, It eases brainstorming with coding. 

"""
Chaos: Discrete Chaos Module for Python
    Copyright (C) 2010  Mehmet Ali ANIL


    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.


    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
"""
__author__ = "Mehmet Ali ANIL"
__copyright__ = "Copyright 2010 GNU-GPL 3.0"
__license__ = "GNU-GPL 3.0"
__version__ = "0.1"
__status__ = "Production"




import os
import sys
import math
import matplotlib.pyplot as plt
import numpy as np


def LogisticMap(x,r):
    """
    Logistic Mapper
    Input arguments: 
        x: the x[n] variable
        r: the r bifurcation parameter
    Returns:
        x[n+1]
    """
    return x*r*(1-x)


def CreateLogisticSeries(n,x,r):
    """
    Creates a logistic Series, where x[k+1] is calculated form x[k]
    from x[0] till x[n-1] with the bifurcation parameter r.
    Input Arguments:
        x: the x[n] variable
        r: the r bifurcation parameter
        n: length of the list generated
    Returns:
        [ListInput,ListOutout]
    """
    Input=[0]*n
    Output=[0]*n
    Input[0]=x
    for nDummy in range(n-1):
        Output[nDummy]=LogisticMap(Input[nDummy],r)
        Input[nDummy+1]= Output[nDummy]
    Output[n-1] = LogisticMap(Input[n-1],r)
    return [Input,Output]


def LogisticsMapBifurcationDiagram(rInitial, rFinal, StepSize, xInitialLogistic, nLogistics):
    Last50Iterations=[]
    XValueTracker=[]


    for r in np.arange(rInitial,rFinal,StepSize):
        Data=CreateLogisticSeries(nLogistics,xInitialLogistic,r)
        DataOutput = Data[1]
        Last50Iterations.extend(DataOutput[499:999])
        XValueTracker.extend([r]*500)


    plt.clf()
    plt.scatter(XValueTracker,Last50Iterations,marker='o',s=0.005)
    plt.grid(True)
    plt.savefig('/home/mali/Desktop/BifurcationDiagram.png',format='png')
    plt.show()


LogisticsMapBifurcationDiagram(2.5,4,0.001,0.4,1000) 


As a result, one can easily obtain this beautiful bifurcation diagram:

 

A bifurcation diagram is obtained with plotting persistent slotions, or equilibrium points with respect to the bifurcaion parameter, that was r in our case. Despite of seeing that a very simple mapping rule turns out to be so exotic and complex in action, it was a relief to see that logistics map has values of r, that it returns to acting like it has finite  points. Just like an eye of a tornado, from a distinctive chaotic behaviour, with a little change in parameter r, it intoduces a silent, predictable state space. 

The code provided does a similar thing, but in a rather crude way. I will optimize it, and play with it more, add more chaotic maps to it, and also add the ability to draw Cobweb diagrams.

I encourage you all to spend some time with Python, it is an easy programming language and it has lot of modules  that can be implemented with an easy line of code. Python can be used in an interactive mode, just like Matlab, so that you can play with your data, or alter it in real time. It is not a Matlab alternative in may ways, but it is fun to use it. 

Add comment


Security code
Refresh