This is a Matlab code which plots the bifurcation diagram for the logistic map $f(x)=ax(1-x)$.
close all, clear all, clc avalues=2.5:0.005:4; N=200; for a=avalues x0=rand(1); % x0=0.1; x=x0; for n=1:100 x=a*x*(1-x); end for n=100:N x=a*x*(1-x); plot(a,x) % plot(a,x,'o','MarkerSize',1) hold on end end
The following is a much faster version of the code above, thanks to Ken Brazier. It takes advantage of the Matlab vectorization operations.
close all, clear all, clc avalues=2.5:0.005:4; N=1000; a=avalues; x0 = []; for i=1:length(avalues) x0=[x0 rand(1)]; % x0=0.1; end x=x0; for n=1:.3*N x=a.*x.*(1-x); end figure hold on for n=.3*N:N x=a.*x.*(1-x); % If you use the following, it will plot lines instead of dots! % plot(a,x) plot(a,x,'.','MarkerSize',0.5) end hold off