Вы находитесь на странице: 1из 3

Introduction to Matlab

Matlab is a type of computer language, which you can use to do the same types of things
(although generally not as quickly) as C, Fortran, etc. However, the real usefulness of Matlab
comes from its built-in functions (programs like matrix inversion that have already been
written for you).
The Command Line. Matlab can be used in command-line mode. This mode is what you
get when you start Matlab from Start Menu Programs. You type in each command at the
prompt (which looks like: >>), then hit enter and it executes the command immediately.
As an example, execute the following commands:
1. At the prompt type: a=1
2. Hit enter
3. At the prompt type: b=2
4. Hit enter
5. Type: a + b
6. Hit enter
You assigned the value of 1 to the variable called a, and the value of 2 to the variable b, and
executed the sum of a and b.
The M-file. You can also put a whole bunch of commands in a file (called an m-file),
and execute all the commands in sequence, without hitting enter between each line, by just
typing the name of the file without the .m.
To do the same sequence of commands as above:
1. From the Matlab file menu item, choose new, then M-file.
2. In the window that pops up, type:
a=1
b=1
a+b
3. From the M-file file menu item, choose save to save the file. Give it a name like:
firsttry.m
4. Go back to the Matlab window, where you see the prompt: >>
5. Type your file name (without the .m) at the prompt: firsttry
6. Hit enter

Entering a Matrix. The matrix starts with an open square bracket [ and ends with a
closed square bracket ]. The entries of a row are separated by spaces (or commas). The
semicolons indicate the start of a new row.
For example, back in command-line mode, at the prompt type:
A = [ 3 2 2; 1 3 -1; 2 6 1]
This command assigns the given matrix to the variable A.
The i,j th entry of A is given by A(i,j). For example, the entry of A in the 2nd row and 3rd
column can be written out by typing:
A(2,3)
Variables in Matlab. When you assign a number or a matrix, Matlab will remember it
until you clear the value, or until you reassign it.
To see which variables are already in use, type: who
The following rules apply to the naming of variables in Matlab:
Matlab is case sensitive. For example, A and a are two different names.
The name may contain up to 19 characters.
The name must start with a letter, which can then be followed by letters, digits, or
underscores.
The name cannot contain any spaces.
Punctuation characters are not allowed.

Assign matrices to the variables A and B, and try the following commands in Matlab:
get the second row of a matrix:

C = A(2,:)

get the third column of a matrix:

C = A(:,3)

to get the 1st row or 2nd column:

what do you type?

Adjoin more columns to A: C = [ A B ]


This will only work if A and B have the same number of rows. This is useful for creating
an augmented matrix.
Adjoin more rows to A: C = [ A; B ]
This will only work if A and B have the same number of columns.
the matrix product:
matrix addition:

C = A*B

C=A+B

powers of a matrix:

C = A2

transpose of a matrix:

C = transpose(A)
2

what does the following do?:


the trace of the matrix:
the inverse of the matrix:

C = A

C = trace(A)
C = inv(A)

get the identity of size 3 3:

I = eye(3)

get the identity of size 4 4:

what do you type?

get the zero matrix of size 3 3:

C = zeros(3)

get the zero matrix of size 3 2:

C = zeros(3,2)

get the zero matrix of size 2 4:

what do you type?

Try the following command:


C=rref(A)
What do you think this does? Hint: rref is an acronym.
Exercise: Solve the following system of equations:
2x1
x1
x1
3x1

3x2
+ 2x2
3x2
+ x2

+ 5x3
x3
+ x3
2x3

x4
+ x4
2x4
+ x4

= 1
= 4
= 1
= 5

Saving your work. If you want to save work to print out (say to hand in as an assignment), you can use the diary command. This has to be initiated before you start entering
commands. So when you start your Matlab session, at the command line type:
diary filename
where filename is replaced by whatever you want to call the file, e.g. test.txt (I recommend
the .txt extension so that it can be recognized as a text file). This will save all commands
and output for that session (or until you type diary off) in a text file in the Matlab work
directory. You can read this file using Word (or whatever) and print it from there, but you
cannot use it to re-execute the commands. Also, note that if you choose a filename which you
have already used, then your work is appended to the end of the file; it doesnt over-write
the file.
Help You can get lots of information (maybe too much) from the Matlab help (click on
the help of the Matlab menu on top of your Matlab window).

Вам также может понравиться