Matlab Tutorial

MOL 410/510 Home | Syllabus | Reading material | Homework & solutions | Notes for Presentations

The three articles below are a primer for types of analysis we will be performing with Matlab during the course.  The first is an introduction to Matlab basics.  The others cover analyzing images and modeling biological processes using differential and stochastic equations.

Matlab-Introduction
Image-Analysis-with-Matlab1
Odes-and-Stochastics-with-Matlab

Students who have not used Matlab before should go through this short tutorial before:

Typing commands at the prompt


To get started using Matlab, open a Matlab window. You should see a prompt that looks like two greater than signs, at which you can enter commands:

  >>

At the prompt, you can type simple mathematical commands, followed by “Enter,” and Matlab will compute them. For example, type the following, followed by “Enter”:

  >> 9*7

or …

  >> cos(pi)

If you saw 63 and -1 after you hit “Enter,” you are on the way!

Try entering in some different mathematical commands. Compute the natural logarithm of 10.

Getting help


If you are wondering if Matlab has some mathematical function, you can simply type “help …” for whatever you are looking for. At the prompt type

  >> help help

One of the functions we will use is one that returns a random number. Type

  >> help rand

Experiment with the rand and randn commands.

M-files


Of course, we don’t want to spend all day entering in commands at the prompt. Matlab provides a simple way of saving and executing multiple commands. If you create a file with the extension “.m”, and type the name of the file, Matlab will execute the commands in the file.

In a text editor create a file named test.m .

Edit the file so that it contains the following:

  9*7
  cos(pi)

At the Matlab prompt, make sure your “current working directory” contains the file test.m You can find which directory you are in by typing

  >> pwd

You can change directories exactly as if you were at a command prompt. Once you have changed your directory to the one that contains test.m, at the command prompt type

  >> test

(Note that the file should be named “test.m”, but at the prompt you enter “test”, without the .m extension.) What do you see? If the file was saved correctly, you should see that Matlab simply executed the lines in the file test.m as if you had entered them at the prompt.

Let’s try a slightly more complicated example. Download the file random_walk1.m into your current working directory. Open the file to see it’s contents, and at the prompt type:

  >> random_walk1

Try running the same program a few times. You can either re-type the filename, or simply hit the up-arrow key to bring up the last-typed command, and hit “Enter.” Another way to execute an M-file, if you are using the Matlab editor, is to simply hit the F5 key.

In order to see how different executions of the same program differ, at the prompt type:

 >> figure(1); hold on

and run the program a few more times. Note that if a line ends with a semi-colon, Matlab executes the command but does not print out the result.

Explore the file random_walk1.m by using the help function. Once you understand all of the commands in this file, try downloading the other Matlab files from the homepage, and explore them.

Loops and Vectors


Loops are used in computing to compute functions over a large number of values. We will use them extensively in the course. The simplest type of loop is one that cycles through a list of numbers and makes a computation on each number. One type of loop is a “for” loop, which has the following format:

       for i = start : finish
            ...computations...
       end

For example, to print out the first 10 integers, type:

  >> for i = 1:10
  i
  end

Notice that the prompt does not appear again until the “end” command is entered. Another way to run the loop is to enter the commands into an m-file and run the file.

Make a loop that computes the first 10 even numbers.

Now suppose we want to save the values we compute in the loop. We can do this by forming a vector. As an example, type the previous lines, but with a small change:

  >> for i=1:10
  a(i)=2*i;
  end

Then type

  >> a

You have created a vector named “a”. It has ten components, which can be referenced by their position. For example, type

  >> a(3)

which returns the third component of a, which is 6.
For a slightly more complicated example, compute the first ten Fibonacci numbers:

  >> a(1)=0;a(2)=1;
  >> for i=3:10
  a(i)=a(i-2)+a(i-1);
  end
  a

Matlab offers several ways of creating vectors. One way is as we saw above, by simply assigning a value to each element in the vector. But, if we want to create a large vector, there are easier ways to do it. Suppose we want to create a vector with the first 10 integers. This can be done by typing

  >> 1:10

A variable can be created by typing

  >> x=1:10

Then, we can reference value of the ith position by typing the variable with the position in parantheses:

  >> x(3)

which, of course, just returns 3.

Create a vector with the integers from 3 to 15.

We can create a vector which has spacing between the entries different from 1 by putting the difference between the beginning and end values. Type

  >> x = 0:2:10

What is x(3)?

Now, suppose we want to create a vector that contains the values from 0 to 1, with step-sizes of 0.1. We can do this by typing

  >> x = 0:.1:1

Another way to do this is with the function “linspace“. This creates a vector with equally spaced points, and is sometimes easier to use than the above method. The format is

       linspace( beginning value, end value, number of points)

For instance, to reproduce the vector x as above, we can type

  >> x = linspace(0, 1, 11)

This form is easier to use if we want to create a vector of points between values that are not integers. For instance, to create a vector of points between 0 and Ï?, type

  >> x = linspace(0, pi, 10)

Plotting Figures


We have already seen some examples of plotting figures when we ran the program random_walk1. Now we will see a few examples of plots and how they work.

With the vector a containing the first 10 Fibonacci numbers as above (re-calculate the vector if needed), type

  >> plot(a)

When you plot a single vector like a, Matlab plots the elements of the vectors against the position of the elements in the vector. In other words, Matlab is plotting the points
(1, a(1)), (2, a(2)), (3, a(3)), etc.
The points along the x-axis are 1,2,3,…, and the elements along the y-axis are the values of the vector a: a(1), a(2), a(3), …

However, we don’t often want to make plots with the integers along the x-axis. Usually, we will want to plot y values against x values. For example, suppose we want to make a plot of sin(x) for x between 0 and 1. We can do this by typing the following:

  >> x=linspace(0,1,11);
  >> plot(x,sin(x))

We can also define a new vector y as follows:

  >> y=sin(x)

and then create the same plot by typing

  >> plot(x,y)

Now we can see why the linspace function is so useful. To make a plot of sin(x) for x between 0 and 2pi, type

  >> x=linspace(0,2*pi,10);
  >> y=sin(x);
  >> plot(x,y)

Now we see that Matlab plots the sine wave over its entire period, but the output is not smooth. This is because Matlab is plotting the points
(x(1),y(1)), (x(2),y(2)), (x(3),y(3)), etc.,
and then connecting these points with straight lines. To make the curve smoother, we must add more points to the curve. For example, type

  >> x=linspace(0,2*pi,100); >>y=sin(x); >>plot(x,y)

This should give you a smooth sine curve.
Type >>help plot and experiment with the various options.
Create a plot of y = 3*sin(x/2) for x between 0 and 4pi.

 

MOL 410/510 Home