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

Sending messages with sendmail

When running long code streams, waiting in front of the computer watching the program run can be excruciating. Sometimes I find myself doing this if I do not know how long the code will run, or if there will be errors returned. (I also sit in front of the computer because I enjoy watching cat gifs and the old school de-fragmentation screens, so who knows…) But you don’t have to! Go out and accomplish something else while your computer chugs away. With the useful command sendmail, we can send messages to our email or phone with some simple commands. Through this post, I’ll share some sample .m files that we commonly use to notify ourselves of code completion or errors.

Continue reading

Managing Your Path in Matlab – Part II

In the last article I wrote about managing your path in Matlab, I covered some of the functions that deal with the search path, including path, matlabroot, addpath, rmpath and genpath.  These functions provide a solid base for viewing, adding and removing directories from your search path.  In this article, I will explain how to use several more functions that deal with your search path, including functions that make changes which persist after ending your Matlab session. Continue reading

Managing Your Search Path in Matlab – Part I

Management of your search path in Matlab is an important skill that every Matlab programmer should have.  Your search path is the ordered set of directories that Matlab uses to find a function that you call.  Being aware of your search path is a good habit, especially if you are working with multiple versions of the same code.  In this post and the following post, I will describe how to use Matlab to modify your search path. Continue reading

Vectorization in Matlab Not Always Necessary

What separates Matlab from many other programming languages is the ability to vectorize code.  Vectorization allows a programmer to write code that is more intuitive, more concise, and often faster than using standard for, while and if statements.  If you have to do a large data processing task or need to create real-time application that does a large number of mathematical operations, vectorization is often a good option.  However, vectorization is not always the faster alternative for time-sensitive tasks.   I created tests for six tasks and compared the amount of time needed to run the vectorized and non-vectorized code.  The results were mixed, showing a decrease in running time for vectorized code on some tasks and a increase in running time on other tasks.  Tests involving mathematical operations ran faster using vectorized code, while tests involving conditional operators and vector creation ran faster for non-vectorized code.

[table id=13 /] Continue reading

How to Do Polynomial Curve Fitting in Matlab

Polynomial curve fitting is a common task for data analysts in many fields of science, engineering and social science.  The standard method to fit a curve to data is to use the least squares method.  In this method, the coefficients of the estimated polynomial are determined by minimizing the squares of errors between the data points and fitted curve.  This method is used to determine the relationship between an independent and dependent variable.  The common term regression line is used for a first-degree polynomial.  Matlab has a simple function called polyfit that allows an analyst to use the least squares method.  In this post, a simple example of polyfit is presented to determine the relationship between two variables in a noisy environment.

Continue reading