Search code examples
matlabmathnonlinear-functions

Logistic map plot is incorrect (Matlab)


I am trying to iterate and plot the Logistic map nonlinear function given by the equation: x[n+1] = 4*x[n]*(1-x[n]). I have found an implementation of the function here https://www.mathworks.com/matlabcentral/answers/122101-plotting-f-x-as-a-function-of-x-logistic-map

I have followed the same thing only the number of points N and the starting initial condition is different in my implementation. I don't know why I am getting no values in the output; mostly are zero value. When the initial condition is x[1] = 0.5, I get the weird plot as given in the picture. But when the initial condition is say 0.3, then I get the proper Logistic map. Theoretically, the initial condition can be any number between 0 and 1. So, why does the code not work when the initial condition is 0.5?

What is the problem?

N=20000; % number of data points
x = zeros(1,N);
x(1) = 0.5; % initial condition (can be anything from 0 to 1)


for n = 1:N
    x(n+1) = 4*x(n)*(1-x(n));
end
 plot(x(1:N),x(2:N+1),'rs-')
 xlabel('x_n')
 ylabel('x_{n+1}')

Here is the plot

plot


Solution

  • The result you're getting is correct. Let me explain it with the cobweb diagram below (source: http://sites.saintmarys.edu/%7Esbroad/example-logistic-cobweb.html)

    cobweb

    The parabola is the curve y = 4*x*(1-x), the blue linear curve is y=x. The way the points x[n] are determined is the following:

    1. start at point (x0,y(x0)) (often the line with (x0,0) is drawn)
    2. go horizontal until you hit the linear curve
    3. go vertical until you hit the parabola. This is your x[n+1].
    4. repeat steps 2-3 until infinity or x[n]=x[n-1] (stable point) or x[n]=x[n-m] (periodicity)

    Note the stable points are where the parabola and linear curve intersect.

    Applying this method to with your parameters gives:

    1. Start in (0.5,1)
    2. go horizontal to (1,1)
    3. go vertical to (1,0) (this is x[1])
    4. go horizontal to (0,0)
    5. go vertical to (0,0) (this is x[2])
    6. go horizontal to (0,0)
    7. go vertical to (0,0) (this is x[3])
    8. stop

    So you just happen to end up the stable point.