Objectives
To learn the fundamentals of working with MATLAB including:
1. How to create a diary file of a MATLAB session.
2. How to enter expressions to be evaluated.
Prerequisites
None
Background
MATLAB (an abbreviation for MATrix LABoratory) is a software package supporting interactive, matrix-based numerical computation for scientific and engineering applications. In addition, it supports graphics and has a built-in high level language. It is powerful, yet easy to learn to use. The best way to learn to use MATLAB is to work with it hands-on. The following tutorial is meant to be done interactively on the computer and provides you with a brief introduction to the fundamental features of the software needed to get started in using it. Since MATLAB is available on several hardware platforms and in different versions (i.e., Student or Professional), there are some differences in the user interfaces. See Appendix A for more information.
Tutorial
Note: In the examples that follow in this section (as well as others), commands that you should type in at the ">>" prompt are bold-faced. (Do not type in the >>). If part of the command is in italics ( for example, >> % your name ) it represents something to be replaced by the appropriate specific value.
1. Start up a session with MATLAB by choosing the MATLAB icon in the math folder.
After MATLAB has been correctly started, the prompt ">>" should appear. That is the signal that the MATLAB interpreter is awaiting expressions to be entered that it will evaluate.
2. A diary is MATLAB's name for a text file containing a record of all of the inputs from the user and the outputs from MATLAB. Assuming that you have a directory already created on the N: drive called m244, create a diary of your session to be saved under the filename "assgn1-1.txt" on the N: drive under the directory m244.
>> diary N:\m241\assgn1-1.txt
3. A "comment" (documentation not to be evaluated by MATLAB) is entered prefaced by a "%" sign. Enter your name, assignment, date (or other information as instructed) as comments:
>> % your name
>> % Lab 1.1 today's date
4. Anything that is not preceded by "%" is treated as an expression to be evaluated. The user enters an expression (followed by a carriage return), and then MATLAB evaluates and prints the result returned to the screen. The fundamental data object for MATLAB is the matrix. An easy way to enter a matrix in MATLAB and to save its value in a variable is to enter
variable = [ list of each row's values separated by ;'s].
The matrix itself is returned as the value of the expression entered. A scalar value is treated as a 1x1 matrix and may be entered without any square brackets. Enter:
>> A = [1 2 3; 4 5 6; 7 8 9]
>> c = 10
5. You may also enter an expression without saving its value in a variable. The most recent value returned by MATLAB is saved in the system variable ans.
Try the following:
>> [1 2 3 4; 5 6 7 8]
>> ans
>> 5+2*30
>> ans
6. MATLAB is case-sensitive That is, "a" is not the same as "A", nor is "aBs" the same as "Abs" or "abs". Enter the following (the command who gives information about currently defined variables and their current values):
>> B = [0 1; 1 0]
>> b = 2.5
>> who
7. When all the entries of a matrix are integers, then they are displayed as such.
If one or more are entered as real (with a decimal point), then the format for the display is real (with decimal points). Enter the following:
>> format long
>> A = [1 2 3.5; 4 5 6]
>> b = 7.921
>> format short
>> A
>> b
8. Determine what version of MATLAB you are running by with the ver comamnd
>> ver
Most of the commands are same for different versions of MATLAB, but there are some exceptions. To display Matrices in rational form, if you are running version 4.0 or higher use the format rat command
>> A = [1/2 1/3 1/4; 1/5 1/6 1/7]
>> help
>> help sqrt
10. Once a command has been entered and executed, it can be recalled, edited if necessary and executed again by using the arrow cursor keys. Enter the following command:
>> C = [1 2 3 ; 5 6 7 ]
Then use the up arrow key to recall the command and edit it with the cursor keys to add a 4 to the end of the first row and an 8 to the end of the second row of C:
>> C = [1 2 3 4; 5 6 7 8 ]
The editing features and keys vary on different systems -- help cedit provides some additional information.
11. Complex numbers are supported. They maybe entered using i, a variable which initially has value sqrt(-1), as in the examples below. Enter:
>> x = 3+5*i
>> C = [ 1+5*i 2+3*i; 4+6*i 3+7*i]
Note: When entering complex values as elements of a matrix within brackets, do not use any blank spaces within the number. Otherwise, it will be treated as separate numbers.
12. MATLAB has a rich set of mathematical functions available and also provides the ability for the user to define new functions for subsequent use. Since you are just getting started with MATLAB, try out some simple functions:
>> x = sqrt(2.5)
>> y = log(x)
>> r = x*cos(pi/6)
>> A = magic(4)
13. All the variables that you have created and their current values are saved in memory (in an area called the workspace). If you want to clear a variable from memory, use the command clear variablename. The command clear by itself will clear all user-defined variables. Try the following:
>> who
>> clear A
>> who
>> clear
>> who
14. Steps for ending a session: If you have a diary file created for your session with the diary command then it should be closed with the command diary off. If you want to save the values of your variables during this session to be restored in the next session (for example, if you must interrupt your session to be finished later), use the command save filename. This causes all variables to be written to the named file. These may be reloaded into the workspace during a subsequent session with the command load filename. The command quit may be used to exit MATLAB. To close your diary file and terminate your current session, type
>> diary off
>> quit
Your diary file (saved in whatever filename you used in your diary command -- here, assgn1-1.txt) is a text file which may be edited with your favorite text editor to clean up and add extra documentation or comments. It may then be printed (or electronically mailed to your instructor if you are on a network).