clear;
min_mu = 0.5;
max_mu = 5;
max_count = 400;
g = 5;
n = 0.9;
k = 300;
max_moves = 30;
min_moves = 1;


for j=1:max_count
    if (j == 1)
        z(j, 1) = 0.65;
    else
        z(j, 1) = z(j-1, max_count);
    end
    mu(j) = ((max_count-j-1) / (max_count-1))*(max_mu - min_mu) + min_mu;
    
    avg(j) = z(j, 1);
    for i=1:max_count-1
        z(j, i+1) = n*z(j, i)-2*tanh(mu(j)*z(j, i))*exp(-3*z(j, i)^2);
        avg(j) = avg(j) + z(j, i+1);
    end
    avg(j) = avg(j) / max_count;
end

min_val = min(min(z));
max_val = max(max(z));

for j=1:max_count    
    for i=1:max_count
        z(j,i) = (z(j,i) - min_val) / (max_val - min_val);
    end
    avg(j) = (avg(j) - min_val) / (max_val - min_val);
end


bins = 100;
curr_count = 1;
num_counts = zeros(max_count*max_count, 2);
for j=1:max_count
    prob_dist(1:100, j) = zeros(1:100, 1);
    num_moves(1:max_moves+1-min_moves,j) = zeros(1:max_moves+1-min_moves, 1);
    for i=1:max_count
        prob_dist(round(z(j, i)*99)+1, j) = prob_dist(round(z(j, i)*99)+1,j) + 1;
        if (avg(j) < 0.5)
            move_dist = floor((abs( avg(j) - z(j, i) ) / (1 - avg(j))) * (max_moves - min_moves) + min_moves);                        
        else
            move_dist = floor((abs( avg(j) - z(j, i) ) / avg(j)) * (max_moves - min_moves) + min_moves);
        end
        num_moves(floor(move_dist), j) = num_moves(floor(move_dist), j) + 1;
        num_counts(curr_count, 1) = j;
        num_counts(curr_count, 2) = move_dist;
        curr_count = curr_count + 1;
    end
    prob_dist(:, j) = prob_dist(:, j) / max_count - ones(100,1)/100000;
end

%plot(prob_dist(1:100, curr_val)

%bar3(num_moves)

hist3(num_counts, [max_count, 30])