1

So I am trying to create a heatmap of the logistic map for lambda =2.5 till lambda 4, showing that some outcomes are more common than others as part of my thesis. However so far I did not came far. I plotted the logistic map, however the heatmap part is a bit of a hassle and I can't find how to do it. So, how do I create a heatmap using the coding that I have?

% Logistics Map 
%       Classic chaos example. Plots semi-stable values of
%       x(n+1) = r*x(n)*(1-x(n)) as r increases to 4.
%
clear
scale = 1000; % determines the level of rounding
maxpoints = 200; % determines maximum values to plot
N = 3000; % number of "r" values to simulate
a = 2.5; % starting value of "r"
b = 4; % final value of "r"... anything higher diverges.
rs = linspace(a,b,N); % vector of "r" values
M = 500; % number of iterations of logistics equation
% Loop through the "r" values
for j = 1:length(rs)

    r=rs(j); % get current "r"
    x=zeros(M,1); % allocate memory
    x(1) = 0.5; % initial condition (can be anything from 0 to 1)

    for i = 2:M, % iterate
        x(i) = r*x(i-1)*(1-x(i-1));
    end
    % only save those unique, semi-stable values
    out{j} = unique(round(scale*x(end-maxpoints:end)));
end
% Rearrange cell array into a large n-by-2 vector for plotting
data = [];
for k = 1:length(rs)
    n = length(out{k});
    data = [data;  rs(k)*ones(n,1),out{k}];
end
% Plot the data
figure(97);clf
h=plot(data(:,1),data(:,2)/scale,'b.');
set(h,'markersize',0.25)
ylim([0 1])
set(gcf,'color','w')

Thanks a lot in advance!

enter image description here

Florisdw
  • 11
  • 2
  • A heatmap is basically a coloured 2D plot, right? Sort of a 2D histogram. There's a build-in function for the latter, and for the former you just need to use some matrix-plotting function and an appropriate colour map. – Adriaan Jun 10 '20 at 10:49
  • 2
    As a sidenote on your coding style: it's considered [a bad habit to use `i` and `j` as variable names](https://stackoverflow.com/q/14790740/5211833), as they denote the imaginary unit in MATLAB and can thus lead to hard to find errors. Second: you're growing your `data` array, which is bad for performance, see [this article on preallocation](https://ch.mathworks.com/help/matlab/matlab_prog/preallocating-arrays.html). Even if you don't know the final size directly, preallocate to something large and strip away the excess for performance. – Adriaan Jun 10 '20 at 10:51
  • The figure produced by the code almost looks like a heatmap of (lambda, outcome) pairs. What plot exactly do you want to obtain? – Luis Mendo Jun 10 '20 at 10:55
  • First of all thanks a lot Adriaan and Luis. I added a graph I picked from google and edited it in the way i'd like it to be @louis. Furthermore one can try to modify their graph in matlab to a heatmap, however this did not change the graph in my case indicating something might be wrong with the data storage perhaps? – Florisdw Jun 12 '20 at 07:50

0 Answers0