1. Introduction to MATLAB
Welcome to the MATLAB component of Math 20F! If you are reading this, you are probably already logged into a PC... if not, clickhere for instructions on that. The next thing to do is run MATLAB and Microsoft Word, you can find instructions for thathere.
Be sure to include your name, section
(e.g. B03), and TA's name
at the top of your Word document. As you complete a lab
assignment you should enter all the commands listed and work through
the Examples. But in your
Word document you only need to include your responses to the Exercises.
These responses should consist of what Exercise you are answering
clearly labeled and then whatever combination of the following four
items is applicable:
Icon: | Item: | Comments: |
Commands entered in MATLAB & resulting output | You should copy this from MATLAB and paste it into your Word document (see Remark 1.1 below). You need only include commands that worked. | |
Plots & graphs | Include all graphs generated in an Exercise unless the problem is specifically telling you which/how many to include. | |
Full sentence response | Exercise contains a question that you need to use at least one or two complete sentences to answer. Even if you're stuck, write down any reasoning or ideas you've had. | |
Requires work by hand | Do scratch work by hand. Describe the result in your Word document. |
Remark 1.1 After highlighting text, there are in general three ways to copy it: (1) choosing Edit -> Copy from the menu bar, (2) clicking the right mouse button and then choosing Copy from the resulting popup menu, or (3) pressing Ctrl-C. We only point this out because the latter options are usually faster and more convenient than the first, but less well known. Likewise, pasting can be accomplished with (1) choosing Edit -> Paste, (2) right-clicking and choosing Paste, or (3) pressing Ctrl-V. Copying plots and figures is slightly different from copying text, as we will point out later in this lab.
You may wish to save your work as you go (in case
of a computer crash, etc.). Click here
for tips on that.
Before we begin, one important note: You are encouraged to talk to your fellow students as you work through a lab. If you're stuck, try introducing yourself to your neighbor. Feel free to discuss responses to difficult exercises. However, in the end, you must submit your own work written in your own words! Identical responses will be penalized at the grader's discretion.
Let's get started. After you've run MATLAB, it should be sitting there with a prompt:
>>
In the Examples, do not type the prompt (>>), simply the commands that follow it (which are listed in red). You can also copy text from the webpage and paste it into MATLAB.
§1.1 Variables in MATLAB
People like to think of MATLAB as a very powerful calculator you can use on your computer. For our purpose, that is not far from the mark.
To define variables, simply type in the variable you wish to define, then an equal sign, and finally the value you wish to assign to that variable. So, if we want to assign the value of 5 to the variable x, we type
>> x=5
x =
5
Unless we later redefine x, every time we type x," MATLAB will replace it with 5 when doing a calculation.
For example, we can define the following variables by their place in the alphabet:
>> l=12; i=9; n=14; e=5; a=1; r=18; g=7; b=2;
We have just assigned specific values to the given letters. (Note: the semicolons in this command suppress the output. If they were not there, once you hit return MATLAB would list the eight variables and their new values.) It is also possible to assign values to more than just single letters using MATLAB. It is sometimes easier to define something with a specific name rather than a single letter. For example, we can define
>> LayText=492
LayText =
492
since our textbook has 492 pages. Notice that there are no spaces in variable names. (We cannot define a variable called "Lay Text".) Also, when defining something, MATLAB is case-sensitive so if we accidentally type laytext and hit return, we will get an error message as seen here.
>> laytext
??? Undefined function or variable 'laytext'.
Using the variables we just defined, we can do some calculations and save them as specific names.
>> linearalgebra
= l+i+n+e+a+r+a+l+g+e+b+r+a
linearalgebra =
105
>> ringingbell = r+i+n+g+i+n+g+b+e+l+l
ringingbell =
109
>> largebill = l+a+r+g+e+b+i+l+l
largebill =
78
Exercise 1.1 Define the letters of your first and last name to be their corresponding number in the alphabet and set your name, without any spaces, equal to the sum of the letters. |
§1.2Matrices in MATLAB
The first thing to learn when dealing with matrices is how to construct a matrix (or vector) using MATLAB.
To define a matrix using MATLAB, we use the square brackets [ ]. [ tells MATLAB we are starting to create a matrix and ] tells MATLAB that we have finished with our construction. The entries of the matrix should be entered in rows from left to right. To separate entries we can either hit the space bar or insert a comma ,. Once we finish with a given row and want to move to the next, we can either press the return key or use a semicolon ;. (This is not the same as using the semicolon to suppress output.)
So, to construct the following matrix
here are two possible ways to go about it:
>> A=[2 1;3 4]
>> A=[2,1(Return-key)
3,4]
Both will yield the desired matrix:
A =
2 1
3 4
Exercise
1.2 Input the following matrix into MATLAB:
|
§1.3 Matrix Operations and Manipulation
Sometimes we need to get at certain parts of a matrix only, maybe just a certain row or even a single entry. This is done with regular parentheses, "(" and ")". For example, to see the (1,2) entry of the matrix A above (i.e. the entry in the first row and second column), we use the command
>> A(1,2)
We also can use the colon ":" to mean 'all', as in the command
>> A(2,:)
which will give us the entire second row of the matrix A. The colon can also be used to represent a range of rows or columns: the command
>> Fibbonacci(2:4,1)
will give us the entries of Fibbonacci from the second through fourth rows in the first column.
A note about matrix operations: two matrices A and B can be added together or subtracted from one another ONLY if they are the same size. (If they are not the same size, MATLAB will produce an error.) Also, two matrices can only be multiplied if their dimensions allow for it; for example, if A is a 2x3 matrix and B is a 3x4 matrix, then A*B will work in MATLAB, but B*A will produce an error.
§1.4 Random Matrices
From time to time, we will be working with random matrices in this class. However, when you are asked to create a random matrix, this does not mean you should just create a matrix using numbers that pop up in your head. Instead, we will let MATLAB do the thinking.
To create random matrices, we use the rand() command.
Notice the parentheses after the command. When using a command that requires some type of input, that input must be put in parentheses. We cannot use square brackets here because when MATLAB sees square brackets, it assumes we are talking about a matrix.
Typing
>> rand(n)
and hitting return tells MATLAB to create a random n x n matrix whose entries are decimal values between 0 and 1.
Now if we type
>> RMatrix1=rand(2)
we will see the following:
RMatrix1=
0.9501
0.6068
0.2311 0.4860
Last Modified:
09/18/04