Random Walk in MATLAB for any Number of Dimensions

Previously, we showed how to perform a random walk in one dimension and in two dimensions using MATLAB’s rand function. Now let’s look at the general case, where the user can specify d > 1 dimensions and n >= 1 step. Here’s a sample gif of how this would look in three dimensions:


Continue reading

Two Dimensional Random Walk in MATLAB

Previously we described what a random walk is and demonstrated some simple code to perform this walk in one dimensional space. Today we will provide some simple code for how to perform such a walk in two-dimensional space. In the following post, we’ll look at the general case, and then we’ll get into some simulations. Here’s a sample of how our output will look like:

2D random walk in MATLAB

2D random walk in MATLAB

Continue reading

One dimensional random walk in MATLAB

We’re going to do a series of posts on Matlabgeeks to demonstrate how MATLAB is a wonderful option for running simulations. As a precursor to this, let’s look at a stochastic process known as a random walk. Briefly, such a process, in one-dimensional space, assumes equal probability of one of two possibilities occurring. For example, if you were to flip a fair coin multiple times, a random walk would demonstrate the total sum of heads and tails that are turned up after n flips. We will demonstrate how to model this process on the integer number line, with heads moving in the +1 direction and tails moving in the -1 direction.

Let’s first setup our function random_walk.m, taking in three input arguments:

% Random Walk in MATLAB
function position = random_walk(numSteps, numDimensions, plotResults)
% Vipul Lugade
% Oct 1, 2018
% matlabgeeks.com

% default values
if nargin < 3
    plotResults = false;
end
% Default the random walk to 2 Dimensions
if nargin < 2
    numDimensions = 2;
end
% Use 40 as default number of steps
if nargin < 1
    numSteps = 40;
end

The ‘numSteps’ variable specifies how many decisions (coin flips in 1D) to perform. The ‘numDimensions’ variable indicates how many dimensions to run the random walk across. For this post we will look at the simple case of 1 Dimensions, but we will show 2D/3D in a future post as well. Finally, the ‘plotResults’ variable is a boolean specifying whether to plot the results of the random walk.

In order to generate a random sequence of events, we will utilize MATLAB’s rand function, which we have previously discussed. For values < 0.5, we will set the value to -1 and for those greater than or equal to 0.5, we will set the value to 1.

% setup random walk values
randValues = round(rand(numSteps, numDimensions));

% One dimensional random walk
if numDimensions == 1
    randValues(randValues == 0) = -1;
% Two dimensional random walk
elseif numDimensions == 2
end

Now, perform the walk and calculate where on the number line we will be after n flips:

% start at origin and initialize the position
position = zeros(numSteps+1, numDimensions);
for k = 2:numSteps+1
    position(k,:) = position(k-1,:) + randValues(k-1,:);
end

Let’s look at an example, when running the following 1 dimensional random walk with 40 steps:

position = random_walk(40, 1, true)

One dimensional random walk in MATLAB

Stay tuned for further posts! If you have any comments or questions, don’t hesitate to contact us.

Creating 2-D polygons in MATLAB

The basic premise of computational geometry is to calculate distances, areas, intersections and other geometrical calculations on basic objects such as points, lines and polygons. To begin this series on computational geometry in MATLAB, we’ll discuss the creation of random polygons in MATLAB.

Continue reading