Introduction to MATLAB



MATLAB stands for MATrix LABoratory. MATLAB is a computer program that can be very helpful in solving the sorts of mathematical problems you will frequently encounter throughout your engineering or technology coursework


1  Starting MATLAB

Starting MATLAB
  1. Press Ctrl-Alt-Delete to begin
  2. Login on to UTAD with your UTAD Username and Passwd
  3. Click ßtart" Þ Programs Þ MATLAB Þ R2006B
    Þ MATLAB R2006 B
  4. First, Change your working directory to your UT H-Drive by the following steps.
    1. Look for Current Directory. Click on the sign "..." (Browse Folder)
      Move down the cursor to find your UTAD H-Drive (yourutadusername.utoledo.edu ...(H:)
      Now your current are under your UTAD H Drive.
    2. Now create a MATLAB directory by (right) clicking your mouse on the blank spot of Current Directory. Then select New => Folder. Then change the name to MATLAB. This is where you save your work. You can create other folders later if you want to organize your files.
Entering and Displaying a Matrix
The MATLAB prompt is >>
To enter a matrix:
  1. start with [
  2. separate elements of the matrix with space (or comma)
  3. use ; (semicolon) to mark end of each row
  4. end the matrix with ]
Example 1 Enter matrix
A = é
ê
ê
ë
1
2
3
4
5
6
ù
ú
ú
û
into work space.
Solution: Type the following, followed by Enter (or return key).

>> A = [  1  2  3  ;  4  5  6]

You should see
A =
   
        é
ê
ê
ë
1
2
3
4
5
6
ù
ú
ú
û
Exercise 2 Enter and display the following matrices in MATLAB.

B = é
ê
ê
ê
ë
1
2
3
4
5
6
7
8
9
ù
ú
ú
ú
û
,
C= é
ê
ê
ê
ë
1
2
3
4
5
6
ù
ú
ú
ú
û
,
u = é
ê
ë
1
2
3
ù
ú
û
and
v = é
ê
ê
ê
ë
1
2
3
ù
ú
ú
ú
û

2  Matrix Operation

MATLAB can be used to perform the matrix algebra. The symbols for standard operations are:
Addition: +     Substraction: -     Multiplication: *     Power: Ù     Transpose: ¢
We will use the matrices we have constructed so far to do the following examples.
Example 1 Compute AB , AT, B2, AC and D = BC. What happen if you try DC?
Solution:
>> A * B
ans =

        é
ê
ê
ë
30
36
42
66
81
96
ù
ú
ú
û
>> A¢
ans =

é
ê
ê
ê
ë
1
4
2
5
3
6
ù
ú
ú
ú
û
>> BÙ2
ans =

é
ê
ê
ê
ë
30
36
42
66
81
96
102
126
150
ù
ú
ú
ú
û
>> A*C
ans =

é
ê
ê
ë
22
28
49
64
ù
ú
ú
û
>> D = B*C
D =

é
ê
ê
ê
ë
22
28
49
64
76
100
ù
ú
ú
ú
û
>> D*C
??? Error using = Þ mtimes
Inner matrix dimensions must agree.
Since DC doesn't make sense, You will see the error information.
Now the matrices C and D have the same dimension. We can do the following example.
Example 2 Compute C+D and 2C - 3D.
Solution:
>> C+ D
ans =

é
ê
ê
ê
ë
23
30
52
68
81
106
ù
ú
ú
ú
û
>> 2*C - 3 *D
ans =

é
ê
ê
ê
ë
-64
-80
-141
-184
-218
-288
ù
ú
ú
ú
û
Don't forget * sign when you multiply a number with a matrix.
Exercise 3 Compute B3 - 15 B2 - 18 B.

3  How to format MATLAB Output

Ordinarily, MATLAB displays up to 5 digits for each number. The following commands allow you to modify this standard display style.
format Standad display
format short Scaled fixed point format with 5 digits.
format long Scaled fixed point format with 15 digits for double and 7 digits for single.
format short e Floating point format with 5 digits.
format long e Floating point format with 15 digits for double and 7 digits for single.
format rat Display the number in fraction as close as possible.
Use help format to see other formatting options.

4  Inverse function

MATLAB has a matrix inverse function inv(A).
Example 1 Find the inverse of the matrix
A = é
ê
ê
ê
ë
1
1
1
5
1
1
2
3
4
ù
ú
ú
ú
û
and also try different formats.
Solution: First, we input the matrix A by

>> A = [  1  1  1  ;  5  1  1 ;  2  3  4].
Then >> inv(A)
You should see
ans =

é
ê
ê
ê
ë
-0.2500
0.2500
0
4.5000
-0.5000
-1.0000
-3.2500
0.2500
1.0000
ù
ú
ú
ú
û
Now try
>> format     rat
>> inv(A).
You should see
ans =

é
ê
ê
ê
ë
-1/4
1/4
0
9/2
-1/2
-1
-13/4
1/4
1
ù
ú
ú
ú
û
.

5  Submatrices, Rows and Columns of a Matrix and other commands

A(i,j): returns the ij entry of the matrix A
A(i,:): returns the i-th row of A
A(:, j): returns the j-th column of A.
A(p:q, r:s): returns the submatrix from row p to row j and column r to column s. (Here p £ q and r £ s.)
rref(A): returns the row reduced echelon form of A.
eye(n): returns a n ×n identity matrix.
zeros(n): returns a n ×n zero matrix.
rand(n): returns a n ×n random matrix.

6   How to Save and Print a Copy of Your Work

You can save a record of every stroke you make, along with MATLAB's responses, in a file you choose by turning on the "diary" feature. For instance, if you select the file name project.txt.
Use the command:
>> diary project.txt
Now let's do something. Say

>> A = [  1  1  1  ;  5  1  1 ;  2  3  4].
Now type >> diary project.txt again.
You can open the file project.txt to see what has been recorded.
You can also use "diary off" to suspend it and "diary on" turns it back on.

7  Scripts M-files

A script M-file is just a sequence of MATLAB commands stored in a text file that has ".m" as its extension. You can use the m editor on MATLAB to create a m file or any other text editor (like notepad but not WORD). After creating a m-file, you just need to type the name of the file to run the command in the m-file.
Example 1
Create a m-file named project1.m that creates a matrix

A = é
ê
ê
ê
ë
1
2
1
1
2
2
2
1
3
ù
ú
ú
ú
û
and compute A3-6A2+5A.
Solution: First find a text editor on your computer.
Type the following:
echo on A = [  1  2  1  ; 1  2  2  ; 2  1  3 ]
AÙ 3 - 6 * AÙ 2 + 5 * A
echo off
Then save the file as project1.m to your working directory. Note that each command is in different lines.
Now type project1. You should see the results. If you skip ëcho on" and ëcho off", you will just see the final results (you won't see A).
In your m-file, you can write comment by adding a percent sign.
For example, you can type
% This is my first MATLAB project.
in the beginning of the project.m file.
Now you can combine the feature of diary and M file to create a report.
Try
>>  diary  project1.txt
>>  project1
>>  diary  project1.txt
Now you have a text file that shows all your work.



File translated from TEX by TTH, version 3.72.
On 20 Feb 2008, 08:35.