Introduction To Matlab: Adapted From
Introduction To Matlab: Adapted From
Introduction To Matlab: Adapted From
Adapted from
http://www.mathworks.com/access/helpdesk/help/techdoc/matlab.shtml
Accessing Matlab
MIT students with Athena account:
On Athena, type add Matlab Then type Matlab &
If you do not have access to one of these accounts, or other access to Matlab
Contact Jeanne Darling, the course secretary, at darling@psrg.lcs.mit.edu and request an account
Try this!
Example: Create a text file that has an array of numbers in it. In your main window, type: emacs mytest.txt & enter some numbers 47, 65, 98, 102, 451 921, 25, 89, 194, 87 2, 10, 74, 66, 200 then press Ctrl-x then Ctrl-c to exit emacs. You will be asked if you want to save the file - say yes. Go into Matlab and type load mytest.txt Then type whos The matrix and its size will be displayed.
Try this!
Type mytest(3,2) Matlab will display ans = 10 Type myvariable = mytest(1,4) Matlab will display myvariable = 102
Try this!
Note: Here weve assigned the value 102 to the variable named myvariable. If you dont assign a variable name, Matlab puts the answer to a query in the variable ans. This gets overwritten every time you type something without assigning it to a name, so beware!
Example: Type mytest(1, 2:5) Matlab displays ans = 65 98 102 451 Type mytest(:, 4:5) Matlab displays ans = 102 451 194 87 66 200 Type myvariable = mytest(end, 1:3) Matlab displays myvariable = 2 10 74
Try this!
Expressions: Variables
Variable names consist of a letter, followed by any number of letters, digits, or underscores. Only the first 31 characters of a variable name are used. MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. A and a are not the same variable. To view the matrix assigned to any variable, simply enter the variable name. To view all variables currently assigned, type who. To view with sizes, type whos.
Expressions: Operators
Operator: Function: Addition Subtraction Multiplication Division Power (x^2 = x*x) Transpose (interchange rows and columns) Precedence
+ * OR .* / OR ./ ^ OR .^ ()
Expressions cont.
Example: Operators work exactly as you would expect when using two scalars, or a matrix and a scalar Type 2+2 ans = 4 Type my_matrix+20 ans = 21 22 23 24 25 26 27 28 29 Type 2*3 ans = 6 Type my_matrix/2 ans = 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 Type 3^3 ans = 27
Try This!
Expressions cont.
Be careful when youre working with TWO matrices! Addition and subtraction work as you would expect, but multiplication, division and exponents dont! Example: Type my_matrix + my_matrix ans = 2 4 6 8 10 12 14 16 18 Type my_matrix*my_matrix ans = 30 36 42 66 81 96 102 121 150 Each element in the first matrix is added to the corresponding element in the second - same for subtraction NOT each element of the first matrix times the corresponding element of the second!
Expressions cont.
In order to get element by element multiplication, division or exponents, use the .*, ./ and .^ operators. Example: Type my_matrix *my_matrix ans = 1 4 9 16 25 36 49 64 81
NOW each element in the first matrix is multiplied by the corresponding element in the second same for division and exponent
Try this!
Expressions cont.
The transpose operator: (single quote) This operator inverts the rows and columns of a matrix. Example: Type mytest ans = 47 921 2 65 25 10 98 89 74 102 194 66 451 87 200 Note that what was the first ROW of mytest is now the first column. What was the second row is now the second column, and the third row is now the third column.
Try this!
Functions
Matlab has built in functions to perform just about any common mathematical operation. These functions include log( ), sqrt( ), abs( ), exp( ) {take the natural log, square root, absolute value, and e to the power} The functions can be used with either a scalar or a matrix as an argument. Example: Type log(my_matrix) ans = 0 1.3863 1.9459 Type log(9265) ans = 9.1721
Try this!