GS 2006/Math - Collins

Lab 1
Introduction to the Computer Lab and MATLAB


Lab Format

The labs will usually consist of some introductory material, some examples to work out in MATLAB, a problem or 2 to solve, and maybe a challenge problem. Most labs will have something that you need to turn in by the end of lab time. You can print out the labs and results if you want or read them online. You can send results to me via email if you want.

How to Read the Lab

In the labs, I'll try to use colors in a consistent way:

MATLAB

We'll be using MATLAB (short for MATrix LABoratory) most of the time in the lab. It is a fairly easy program to use, but is also very powerful. You can use it interactively (like a fancy calculator), but you can also use programs. Most of the time, if there is a program involved, I'll provide the code. You are, however, always welcome to try to program it on your own.
For this Lab, it would be good to try everything in blue or green
Starting Up and Working With MATLAB
Go the Start menu and select MATLAB 7 under the Programs/Matlab menu.
Resize the windows for your browser and MATLAB so you can easily switch between the two.
To Exit, type quit or exit or choose Exit from the file menu (if available).

There are several windows; you'll work mostly with the Command Window by typing commands at the prompt and reading the results. The other windows tell you about your file, variables and previous commands. If you want you can close all the windows except the Command Window. You can restore the windows by selecting them under the View menu.

MATLAB has several built-in help commands:

You can use the up and down arrows to go through the command you previously typed.

Variables
Variable names can have letters and numbers (first character must be a letter). MATLAB is case-sensitive so A1 is different from a1. MATLAB automatically creates the variables. Try these: (hit return after each one)

x = 1 
x = 'hi' 
x = 2

Display
You can display a variable by either typing its name or by using disp.
MATLAB always stores numbers to an accuracy of about 15 digits, but you can control what it shows using the format command. Try these:

x = pi 
format long e 
disp(x) 
format short 
disp(x) 
format rat 
x 
format short 
x

Changing the format only changes how it is displayed, not the value

Matrices
Enter matrices starting with a [ and ending with a ]. Elements in a row are separated by a comma or a space. Rows are separated by a return or by a semi-colon (;).
For example you can enter the matrix
         ( 1  2  3 )
         ( 4  5  6 )
         ( 7  8  9 ) 
in any of the following ways:

A =[1, 2, 3; 4, 5, 6; 7, 8, 9] 
B=[1 2 3; 4 5 6; 7 8 9] 
C = [ 
     1 2 3 
     4 5 6 
     7 8 9 
     ]

You can also build matrices by putting a value in each place. Like

D(1,1) = 1 
D(1,2) = 2 
D(1,3) = 3 
D(2,1) = 4 
D(2,2) = 5 
D(2,3) = 6 
D(3,1) = 7 
D(3,2) = 8 
D(3,3) = 9

NOTE: MATLAB will automatically make your matrix big enough to hold the elements you specify. It will set unspecified entries to 0

If you tried this stuff you probably noticed that MATLAB displayed the results after every command you gave it. This is great when we are learning to use MATLAB, but it gets pretty annoying later. If you end a command with a semi-colon, then the results will not be displayed. Try typing: E(3,4) = 1; (don't forget the semi-colon)

What do you think the result is? Type E or disp(E) to see the result. Are you surprised?

If the elements are defined by a formula, you can write a little program, for example:

for i = 1:3 
   for j = 1:3 
      F(i,j) = i+j; 
   end
end
disp(F)

Some matrix types are built into MATLAB. Try these:

M = magic(5) 
Z = zeros(4,3) 
I = eye(5) 
W = ones(3,2) 
R = rand(3) 
Each of these takes either 1 or 2 numbers for the size of the matrix.
Saving Your Work
There are several ways to save the work you do in MATLAB. The easiest is to keep a transcript of all that you do with the diary command. Type: help diary to learn about it.

Another way is to open a text file either through MATLAB (it would be a M-file) or through your favorite word processor and then copy the good stuff into the text file.

Mail: ccollins@math.utk.edu