All You Need to Know about MATLAB - UMD
All You Really Need to Know about MATLAB
(at least for ENCE 201)
Charles W. Schwartz August 2004
The purpose of these notes is to give you a "bare bones" summary of the subset of MATLAB features that will be most useful for the programming exercises in ENCE 201. MATLAB is a very powerful computational tool that offers a wealth of features beyond those described here. You should consult the MATLAB documentation and Demonstration programs for an overview of these additional advanced features.
DOCUMENTATION
For details (syntax, usage, etc.) and information on more advanced MATLAB features:
? Help menu from main MATLAB window ? Online at
Other useful information: ? Demos from MATLAB Start button ? "Getting Started" section online, accessible via link from page
DATA TYPES By default, all constant and variables in MATLAB are double precision floating point. All computations are also performed in double precision by default. Double precision floating-point numbers in MATLAB have a finite precision of roughly 16 significant decimal digits and a finite range of roughly 10-308 to 10+308.
VARIABLES
Variable names consist of a letter, followed by any number of letters, digits, or underscores. MATLAB uses only the first 31 characters of a variable name. MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. A and a are not the same variable. To view the value(s) assigned to any variable, simply enter the variable name in the Command window.
MATRICES
A matrix is entered as a list of its elements following a few basic conventions: ? Separate the elements of a row with blanks or commas. ? Use a semicolon, ; , to indicate the end of each row. ? Surround the entire list of elements with square brackets, [ ].
1
For example, type in the Command window:
A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
MATLAB displays the matrix you just entered:
A =
16
3
2 13
5 10 11
8
9
6
7 12
4 15 14
1
The element in row i and column j of A is denoted by A(i,j). If you try to use the value of an element outside of the matrix, it is an error. On the other hand, if you store a value in an element outside of the matrix, the size increases automatically to accommodate the new element.
A scalar is equivalent to a 1x1 matrix. In this case, the square brackets are not required: A = 16
OPERATORS
Arithmetic Operators
Expressions use familiar arithmetic operators and precedence rules.
+ Addition - Subtraction * Multiplication / Division \ Left division (see "Matrices and Linear Algebra" in MATLAB ^ Power ( ) Specify evaluation order
Matrix Operators
+ * inv(A) det(A) ( )
Addition Subtraction Matrix multiplication Transpose (e.g., A) Matrix inverse Matrix determinant Specify evaluation order
2
Relational Operators
Operator < >= == ~=
Description Less than Less than or equal to Greater than Greater than or equal to Equal to Not equal to
Logical Operators
Operator & | ~
Description And Or Not
Colon Operator
The colon, :, is an important operator specific to MATLAB. It occurs in several different forms. The expression
1:10
is a row vector containing the integers from 1 to 10
1
2
3
4
5
6
7
8
9 10
To obtain non-unit spacing, specify an increment. For example,
100:-7:50
is
100 93 86 79 72 65 58 51
Subscript expressions involving colons refer to portions of a matrix.
A(1:k,j)
is the first k elements of the jth column of A.
STATEMENTS AND EXPRESSIONS
Arithmetic expressions are written and evaluated in MATLAB much the same as they are in Excel. An example of a MATLAB statement that assigns the result of an arithmetic expression to the variable rho:
rho = (1+sqrt(5))/2 rho =
1.6180
3
Matrices and scalars can be combined in expressions. For example, a scalar is subtracted from a matrix by subtracting it from each element:
B = A - 8.5 B =
7.5 -3.5
0.5 -4.5
-5.5 1.5
-2.5 6.5
-6.5 2.5
-1.5 5.5
4.5 -0.5
3.5 -7.5
for the matrix A given earlier under the MATRICES section.
If you simply type a statement and press Return or Enter, MATLAB automatically displays the results on screen. However, if you end the line with a semicolon, MATLAB performs the computation but does not display any output.
If a statement does not fit on one line, use an ellipsis (three periods), ..., followed by Return or Enter to indicate that the statement continues on the next line. For example,
s = 1 -1/2 + 1/3 -1/4 + 1/5 - 1/6 + 1/7 ... - 1/8 + 1/9 - 1/10 + 1/11 - 1/12;
Blank spaces around the =, +, and - signs are optional, but they improve readability.
BUILT-IN FUNCTIONS
MATLAB provides a large number of standard elementary mathematical functions. MATLAB also provides many more advanced mathematical functions. For a list of the elementary mathematical functions, type
help elfun For a list of more advanced mathematical and matrix functions, type
help specfun help elmat Several special functions provide values of useful constants.
pi
3.14159265...
eps
Floating-point relative precision, 2-52
realmin realmax
Smallest floating-point number, 2-1022 Largest floating-point number, (2-)21023
Inf
Infinity
NaN../../techd Not-a-number
INPUT/OUTPUT Console I/O (i.e., Keyboard and Screen) Keyboard Input The input function displays a prompt and waits for a user response. Its syntax is
4
n = input('prompt_string') The function displays the prompt_string, waits for keyboard input, and then returns the value from the keyboard. If the user inputs an expression, the function evaluates it and returns its value.
Pausing Execution
Some M-files benefit from pauses between execution steps. The pause command, with no arguments, stops execution until the user presses a key. To pause for n seconds, use
pause(n)
Screen Output
disp(X) displays an array, without printing the array name. If X contains a text string, the string is displayed. Another way to display an array on the screen is to type its name, but this prints a leading "X =," which is not always desirable.
For example, use disp in an M-file is to display a matrix with column labels:
disp('
Corn
disp(rand(5,3))
Oats
Hay')
which results in
Corn 0.2113 0.0820 0.7599 0.0087 0.8096
Oats 0.8474 0.4524 0.8075 0.4832 0.6135
Hay 0.2749 0.8807 0.6538 0.4899 0.7741
(See also format and sprintf for screen output with greater format control.)
File I/O
The load function text files containing numeric data. The text file should be organized as a rectangular table of numbers, separated by blanks, with one row per line, and an equal number of elements in each row. For example, create a text file (e.g., using the Windows Notepad editor) containing these four lines.
16.0 5.0 9.0 4.0
3.0 10.0
6.0 15.0
2.0 11.0
7.0 14.0
13.0 8.0
12.0 1.0
Store the file under the name magik.dat. Then the statement
load magik.dat
reads the file and creates a variable, magik, containing the example matrix.
For more powerful file input/output capabilities (i.e., different file structures, binary files, etc.) see the "Programming and Data Types..Input/Output" in the online MATLAB documentation at .
5
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- what is matlab clarkson university
- all you need to know about matlab umd
- creating and running script file tutorialspoint
- candidate information matlab simulink
- a beginner s guide to loyola university maryland
- get started with text analytics toolbox matlab
- matlab 7 mat file format
- matlab workshop 13 linear systems of equations
- matlab editor window command window
Related searches
- things to know about investing in stocks
- what to know about annuities
- what to know about the stock market
- everything to know about stocks
- everything you need to know about stocks
- things to know about annuities
- need to know business terms
- things to know about earth
- everything to know about guns
- everything to know about diabetes
- everything to know about type 1 diabetes
- what do you need to know in public speaking