You are on page 1of 9

IB 115 Lab #1: An Introduction to Matlab

This lab is divided into 2 parts.


Part 1 is for students that are new to Matlab or wish to revise Matlab. Students who choose Part
1 do not need to do Part 2.
Part 2 is for students who have experience with Matlab. Students who choose Part 2 do not
need to do Part 1.
____________________________________________________________________________
Part 1.
You must hand in question 17 and 19 for grading.

We will be using Matlab to generate simple programs that will simulate and provide meaningful
(we hope!) outputs from our models of biological systems. Matlab is easy to use and most
students become fairly proficient after a few weeks. No prior programming experience is
needed. Beginning today and over the course of the semester we will provide you with all the
programming know-how needed to complete the labs and your term project.
Matlab basics:
Here are a few basic Matlab rules to get you started.

Semi-colon
1. At the command prompt (which looks like >>) type: Hello world and press Enter.
You should get the following response:
ans =
Hello world
2. Now retype Hello world with a semi-colon after it and press Enter, i.e. Hello world;
Now you should get:
>>
As you can see the semi-colon stops Matlab displaying the input on the screen which will help
our programs run faster. It also works for the output.

3. Type 3+4+5 at the command prompt and press Enter (note there are no quotation marks).
You should get the response:
ans =
12
4. Repeat the above input followed by a semi-colon i.e. 3+4+5;
You should get:
>>

Note that the quotations marks were used for Hello world but not for the latter example. The
quotation marks tell the program that it is text and that it should not try and make sense of it.
Type 3+4+5 and press Enter and see what happens.

The percent symbol


The percent symbol simply tells the program to ignore the text that follows it so that you can
write comments in the code to remind yourself and others what you are doing. For example if
you type: %My program for exponential growth;

It will be ignored. However, if you forget the percent sign you will get an error because the
program tried to interpret it:
??? Undefined function or method 'My' for input arguments of type
'char'.
Mathematical operations
+

addition

subtraction

multiplication

division

x^y

x to the yth power

exp(x)

e raised to the power x

log

logarithm base e

2e-4

2 x 10-4

Defining Variables
Lets create variables to use in equations. For example, in exponential growth we might want to
use the variables Ni, k and t in the following equation:

Nt = Ni ekt

[In Matlab we would write this as Nt=Ni*exp(k*t)].


5. To define Ni and k we simply type:
Ni=1; Enter
k=2; Enter
t=0; Enter
Nt=Ni*exp(k*t) Enter (note : In this line the semi-colon was not included because we want to
see the output).

You should see:


Nt =
1
6. Because we have defined the variables we can quickly re-calculate Nt. For example, type:
t=2; Enter
Nt=Ni*exp(k*t) Enter

The output is:


Nt =
54.5982
We dont need to retype what k and Ni are because we have already defined them and the
program remembers them. It is for this reason that it is really important when you run a program
that you should type clear all at the beginning. Otherwise the computer can use old variables.

******{Heres a helpful tip: if you push the up arrow you can scroll through previous commands
you have typed. This saves you having to retype it. Try it now.}

Vectors and Matrices:


Numbers can easily be stored in vectors and matrices. Remember a vector has only one row or
column whereas a matrix has multiple rows and columns. Square brackets tells the computer
that it is a vector or matrix.
7. Type >> a=[1, 3, 5, 7, 9]

You should get:


a =
1

8. Try writing the same vector with semi-colons between the elements. What happens?

To make a matrix, simply use commas to separate the elements of a row and semi-colons to
separate rows.
9. Try entering b=[1, 2, 3; 4, 5, 6; 7, 8, 9] , it should produce
b =
1

10. We can tell the computer that we only want to retrieve a specific value, for example, the third
element in vector a using the command a(3). Please try it.
11. Similarly, for a matrix to get the element in the second row of the third column we type
b(2,3). This should give you 6.
12. We can swap the rows and columns by simply writing b=b. This tells us we want to make a
new matrix b where the columns and rows are transposed. Try this and you should get

b =
1

Storing numbers
Often in simulations we want to do a calculation and then store the results and then move on
and to do further calculations and then store these results and so on. We can use matrices and
vectors to store these numbers.
13. Please type
c(1)=1;
c(2)=6;
c(3)=16;
c(4)=21;
This stores values of 1, 6, 16 and 21 at positions 1, 2, 3 and 4 in vector c. Check this by typing c
and Enter.

Similarly, if we want to store values for different components of a system we can store them in
different rows of a matrix.

Note: Matlab is case sensitive. Upper case C is not equivalent to lower case c.
14. For example we take measurements at times 0, 5, 10, 20 minutes.
Please enter this as time=[0, 5, 10, 20];

At these time points we measured the number of bacteria in a population to be 1, 2, 8, 32 and


the number of AHL molecules secreted by them to be 4, 8, 32, 128. We can store the bacteria in
the first row by the command M(1,:)=[1, 2 8, 32] and AHL in the second row with the command:
M(2,:)=[ 4, 8, 32, 128]. This gives
M =
1

32

32

128

See we have now stored all our values in a convenient format. Note the colon : in the above
commands tells the program we are specifying the values for the whole row rather than a
specific column.

Graphs
15. To plot matrix M as function of time (which we entered above), please type
plot(time,M)
140

120

100

80

60

40

20

10

12

14

16

18

20

This graph shows time on the x axis and the bacteria and the amount of AHL on the y-axis.
16. To label the x and y axes use the commands
xlabel(Time)
ylabel(Number)

A legend can be added with the command


legend (Bacteria, AHL)

The order of the labels in the legend command needs to correspond to the order of the rows.

Use the hold function to add additional graphs to the same plot
>>hold on

Creating a program
So far we have entered all of our commands at the command line. For simulations we need to
create a program. In Matlab this is done by selecting from the menu: File>new >M-file. Please
do this and save your empty file somewhere you can find it. A good place in this case is the
default directory to find this simply type: cd

Directories:
Matlab will look for functions that you call from the command line in the current working
directory. The current directory can be changed using the Current Directory pull-down menu
above the Command Window.

For loops
Often in programs we want to get the computer to do a calculation over and over again. Lets
make a program that creates a Fibonacci series (0, 1, 1, 2, 3, 5, 8, 13...). If you are unfamiliar
with this series try and find the pattern. The Fibonacci series is a very common series in
biology and is observed in the spirals of shells, tree branching and the arrangement of florets.
17. Type the following in your empty program file and then save it;

%Fibonacci program;
F=[0,1];
for i=1:20; % perform the commands listed between for and end 20 times
F(i+2)=F(i+1)+F(i);
end
F

Run your program by typing its name and press Enter. Make sure you are in the same directory
as your file. Do you see how the program works? Explain in words what each line is doing and
how it all works together.

18. We can use the while command instead of the for loop. An advantage of the while
command is that the step size can be variable and that we can get the program to stop as soon
as it has reach a specific target. Heres an example:
G=1
while G<10000;
G=G*5
end
Type it and observe what it does.
19. Heres a chance to practice what youve learnt.

Create a Matlab routine that shows discrete exponential growth without constraint. Assume that
we start with a single bacterial cell that divides in two for 20 generations.
Plot the growth of the colony over time.
Label your axes and include a legend.
Print out the graph and code and hand them both in for grading.

If you need help please ask the GSI or a fellow student!

Congratulations, you have mastered the basics of Matlab!

Part 2.
You must hand in question 1 for grading.
1. Write a Matlab program to simulate the daily fluctuations in bioluminescence output by a
population of V. fischeri in the light organ of a bobtail squid. Print-out your code and plot.
Hand them in for grading.
2. You may choose to gain a bonus point by helping other students during this lab who are new
to Matlab. Notify the GSI if you wish to do this. Thanks!

The END

You might also like