GS 2001/Math - Collins

Lab 1 Introduction to 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:
Red for links to files and important messages
Blue for things you type in MATLAB
italic for things you type in MATLAB that
are variable, eg. ... type your name: Your NameGreen for programs and other things you need
to copy into MATLAB or a file
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 greenStarting Up and Getting Help in MATLAB
To Start:. From Windows: Go under the Start menu and select
MATLAB 5.3 under the Programs/Matlab menu.
You might want to resize the windows for you 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)
MATLAB has several built-in help commands:
If you know the name of the command you can use
help command-name
for example, type
help magic
If you don't know the name, but have an idea of what it should
do, you can use
lookfor command-idea
for example, type
lookfor transpose
To search for commands by function, you can use the
help window, type
helpwin
and then double-click on any topic to get a list of
relevant commands
For general MATLAB information you can use the help desk.
This brings up a browser window and gives access to information
about commands, programming, etc. Type
helpdesk
Bring up the help desk in MATLAB.
Click on Getting Started for some general information.
Work through a few pages of the Getting Started tutorial.
Put the browser on one side of the screen and the window
running MATLAB on the other and try out a few commands.
Try out the following areas:
Matrices and Magic Squares Expressions Working with MatricesThe Command Window
Another way to explore MATLAB is to type:
intro
and click on Autoplay for a fast show, to get an idea
what MATLAB looks like.
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.
Matrices in MATLAB
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 )
A = ( 4 5 6 )
( 7 8 9 )
in any of the following ways
A =[1, 2, 3; 4, 5, 6; 7, 8, 9]
A =[1 2 3; 4 5 6; 7 8 9]
A = [
1 2 3
4 5 6
7 8 9
]
You can also build matrices by putting a value in each place. Like
A(1,1) = 1
A(1,2) = 2
A(1,3) = 3
A(2,1) = 4
A(2,2) = 5
A(2,3) = 6
A(3,1) = 7
A(3,2) = 8
A(3,3) = 9NOTE: 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: B(3,4) = 1; (don't forget the semi-colon)
What do you think the result is? Type B or disp(B) 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
C(i,j) = i+j;
end
end
disp(C)
This example shows another powerful part of MATLAB: specifying lists
of numbers. The colon (:) is a powerful tool in MATLAB. Type
1:10
2:2:10
1:0.1:2
2:-0.1:1
We could use it to enter our first matrix as:
A = [1:3;4:6;7:9];
You can also use the colon to specify parts of a matrix. Type
M = magic(5) (This is the 5x5 magic square)
M(1,:)
M(:,4)
M(1:2,2:3)
M(:,:)Matrix Arithmetic and Functions
MATLAB uses +, -, * for matrix addition, subtraction and multiplication.
It will give an error message if the dimensions are wrong. Try the following:
A = rand(3) (random 3x3 matrix)
B = rand(3,4) (random 3x4 matrix)
C = rand(3)D = rand(4)
A*B
B*A
A*C
C*A
A+C
A+D
A*B*D
D*D
C-A
MATLAB also has functions which work on matrices. Try
max(A) (finds the maximum of each column)
sum(B) (finds the sum of each column)
cos(C) (finds the cos of each element)
D.^2 (squares each element, compare to D*D)
To Turn In:
1. Create a 6x6 magic square; call it A.
2. Compute the sum of each column.
3. What MATLAB command would you use to
a. Raise each element of A to the 3rd power.
b. Display the 4th row of A.
c. Display the 2nd column of A.
d. Display the element in the 3rd row and 3rd column.
Challenge
4. Use MATLAB to sum each row.
5. Use MATLAB to find the product of the elements in each column.
Either email the results or print them out and give them to me
before you leave today.