Hi Alakesh,
From what I understand, you are trying to simulate a Lévy walk in 2D, but in order to achieve a better simulation result such that the step length distribution has the characteristic heavy tails of a Lévy walk, you can consider some improvements:
- Generating Levy Walk: Lévy walks are characterized by having step lengths that follow a heavy-tailed distribution, which means the step lengths L are distributed according to a power law, where the probability density function of step lengths L is proportional to L^(−(α+1)) for a Lévy exponent α, which has been done using the following line of code in the given snippet:
t=round(abs((rand()).^(-1/alpha))./dt);
- Calculating Step Lengths: Your step length distribution might be off because you are calculating the step lengths from ‘dx’ and ‘dy’ but then treating these as a single combined distribution. Since step lengths in a Levy Walk should be analyzed in terms of their magnitude, you should calculate the ‘step_lengths’ directly as:as Euclidean distances:
step_lengths = sqrt(dx.^2 + dy.^2);
Whereas your current code is doing:
- Plotting Histogram: You need to calculate the histogram of the step lengths and use appropriate bins to accurately capture the heavy tails, instead of calculating the histogram of ‘dx’ and ‘dy’ which are the differences between consecutive positions, since these represent the x and y components of the step, not the magnitudes.
Further, you can use ‘histcounts’ or ‘histogram’ function to plot the distribution of step-lengths and fit it to a power law, by modifying your code to plot the step length distribution:
title('Trajectory of Lévy Walk');
step_lengths = sqrt(dx.^2 + dy.^2);
histogram(step_lengths, 'Normalization', 'pdf', 'BinEdges', logspace(log10(min(step_lengths)), log10(max(step_lengths)), 50));
title('Step Length Distribution');
ylabel('Probability Density');
pd = fitdist(step_lengths', 'Lognormal');
x_values = logspace(log10(min(step_lengths)), log10(max(step_lengths)), 100);
y_fit = pdf(pd, x_values);
semilogy(x_values, y_fit, '-r', 'LineWidth', 2.0);
Plot the histogram of step lengths directly rather than fitting a normal distribution and use logarithmic bin edges for the histogram to better capture the heavy tails of the distribution.
For more information regarding usage of ‘logspace’ to generate logarithmically spaced vector points or log-scale plots using ‘loglog’ function in MATLAB, refer to the documentation links mentioned below: