Matlab

The following code shows how to calculate the basins of a two-dimensional noninvertible map using the meshgrid function of Matlab. The input variable is not a single point (as usual) but a matrix of point (the entire phase space). This trick makes the process faster. Example taken from:
C. Mira, A. Agliari, L. Gardini, From the box-within-a-box bifurcation structure to the Julia set. PartII: Bifurcation routes to different Julia Sets from an indirect embedding of a quadratic complex map. International Journal of Bifurcation and Chaos, Vol. 19, No 10 (2009) 3235-3282.
function Basin
% Plot the Basins of a map %
%%%%%%%%% axis %%%%%%%%%
xmin = -2;
xmax = 2;
ymin = -0.2;
ymax = 3;
%%%%%%%%%%%%%%%%%%%%%%%%
% Number of iterations %
N = 130;
%%%%%%%%%%%%%%%%%%%%%%%%
%% Set the parameters %%
c = 1.36;
L=0;
%%%%%%%%%%%%%%%%%%%%%%%%
 
%%%%%%%%%%%%%%%% phase plane %%%%%%%%%%%%%%%%%
dx = (xmax-xmin)/1000; dy = (ymax-ymin)/1000;
x = [xmin:dx:xmax];  y = [ymin:dy:ymax];
[X,Y] = meshgrid(x,y);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%%%%%%%%% Map iteration %%%%%%%%
Z3=X; Z4=Y;
for j = 1:N,
    [Z3, Z4] = MAP(Z3,Z4,c,L);
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%%%%%%%%%%%% Final Test %%%%%%%%%%%%%%%%%%%
Z31=10>abs(Z3);
Z41=10>abs(Z4);
Z51=(Z31|Z41);
Z33=((((0.072<abs(Z3))&(0.074>abs(Z3))))|((1.3<abs(Z3)&1.36>abs(Z3)))|((0.45<abs(Z3)&0.48>abs(Z3)))|((1.12<abs(Z3)&1.14>abs(Z3))));
Z43=(0<=abs(Z4))&0.1>abs(Z4);
Z53=(Z33&Z43)*2;
Z34=((((0.7<abs(Z3))&(0.74>abs(Z3))))|((0.59<abs(Z3)&0.61>abs(Z3)))|((0.31<abs(Z3)&0.33>abs(Z3)))|((0.43<abs(Z3)&0.45>abs(Z3))));
Z44=((((0.4<abs(Z4))&(0.42>abs(Z4))))|((0.27<abs(Z4)&0.29>abs(Z4)))|((0.63<abs(Z4)&0.65>abs(Z4)))|((0.82<abs(Z4)&0.85>abs(Z4))));
Z54=(Z34&Z44)*4;
Z5=Z51+Z53+Z54;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%%%%%%%%%%% plot the basins %%%%%%%%%%%%%%%
figure(3);
colormap hsv(5);
pcolor( X,Y,abs(Z5));  shading flat;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
% ************ Quadratic Complex Map *************** %
function [Z1, Z2] = MAP(X,Y,c,L);
Z1 = X.*X+Y-c;
Z2 = L*Y+4*X.*X.*Y;
return
% ************************************************** %
 

Figure:
Basins.jpg



The Mira Map basins using Matlab (by Davide Radi). Example taken from: Sulle Orme del Caos (Comportamenti complessi in modelli matematici semplici), Gian Italo Bischi, Rosa Carini, Laura Gardini, Paolo Tenti, Bruno Mondadori, pp.205-210.
%%%% The MIRA MAP %%%
function BasinsMiraMap
%%%%%%%%% axis %%%%%%%%%
xmin = -2;
xmax = 2;
ymin = -2;
ymax = 2.2;
%%%%%%%%%%%%%%%%%%%%%%%%
% Number of iterations %
N = 40;
%%%%%%%%%%%%%%%%%%%%%%%%
%% Set the parameters %%
a = -0.3;
b = -1.5;
%%%%%%%%%%%%%%%%%%%%%%%%
 
%%%%%%%%%%%%%%%% phase plane %%%%%%%%%%%%%%%%%
dx = (xmax-xmin)/400; dy = (ymax-ymin)/400;
x = [xmin:dx:xmax]; y = [ymin:dy:ymax];
[X,Y] = meshgrid(x,y);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%%%%%%%%% Map iteration %%%%%%%%%%
Z3=X; Z4=Y;
for j = 1:N,
[Z3, Z4] = MIRAMAP(Z3,Z4,a,b);
end;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%%%%%% Final Test %%%%%%%%
Z3=10>abs(Z3);
Z4=10>abs(Z4);
Z5=(Z3|Z4);
%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%% plot the basins %%%%%%%%%%%%%%%%%%%%%%%
figure(2);
colormap bone; % alternatively: (hsv(5)) or (jet)
pcolor( X,Y,abs(Z5)); shading interp;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
%%%%%%%%%%%%%%%% Chaotic Attractor %%%%%%%%%%%%%%%%%%%
N=2000;
At1=zeros(N);At2=zeros(N);
At1(1)=0.1; At2(1)=0.1;
for j = 1:N,
[At1(j+1), At2(j+1)] = MIRAMAP(At1(j),At2(j),a,b);
end;
hold on; plot(At1,At2,'.','markersize',1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
% ************* MIRA MAP ******************** %
function [Z1, Z2] = MIRAMAP(X,Y,a,b);
Z1 = a*X+Y;
Z2 = b+X.*X;
return
% ******************************************* %

Figure:
MiraBasins.jpg



Generate a dataset (txt file) with a programming language of your choice and draw the basins of attraction with Matlab. For more details send an email to radidavide85@gmail.com.
% Open the txt file created with VB or other.....
Finput = fopen('C:\BacinoAttrazione.txt');
Fl='C:\BacinoAttrazione.txt';
% Clean
clc; clf; clear All;
% Define graph
colordef white;
hold on;
axis normal;
Box on;
% Label axes
xlabel('VISITORS BUY THE EC');
ylabel('FIRMS BUY THE EP');
% Create a zeros array with the number of entries equal to ...
% the number of line of the txt file (the dataset)
line=zeros(401, 401);
% Read txt file line by line
line = textread(Fl,'%d',-1);
% Upload the data stored in txt file in the array generated
l=0;
for j=1:1:401;
    for i=1:1:401;
        l=l+1;
        M(i,j)=line(l);
    end
end
% Using meshgrid create a virtual representation of the Phase Portrait
xmax=1;xmin=0;
ymax=1;ymin=0;
dx = (xmax-xmin)/400; dy = (ymax-ymin)/400;
x = [xmin:dx:xmax];  y = [ymin:dy:ymax];
[X,Y] = meshgrid(x,y);
% Generate colormap
% (the number of colors defined in map must match the number of basins of attraction)
% for example grayscale as follows:
map(1,:) = [.5 .5 .5]; % pure gray % good attractor
map(2,:) = [1 1 1]; % pure white % devil attractor
map(3,:) = [.0 .0 .0]; % is black % subgood attracor
map(4,:) = [.8 .8 .8]; % is durk gray % other ....
colormap(map);
% draw the basins of attraction
pcolor( X,Y,abs(M));  shading interp;
Example: This txt file has to be stored in C:\ so it can be direcly invoked by the above .m matlab file. This file can be run and you get the picture below.

BacinoAttrazioneAG.JPG