BasicMDL 2 Fundmtls
BasicMDL 2 Fundmtls
Dominic C. Ezugworie
[email protected]
+234 (0) 703 907 1904
Exercises
2
Numbers and Arrays (Matrices 1)
Numbers
• MATLAB uses conventional decimal notation, with an
optional decimal point and leading plus or minus
sign, for numbers.
3 -99 0.0001 9.6397238
• Scientific notation uses the letter e to specify a
power-of-ten scale factor.
1.60210e-20 6.02252e23
• Imaginary numbers use either i or j as a suffix.
1i -3.14159j 3e5i
3
Numbers and Arrays (Matrices I)
• In the MATLAB environment, a matrix is a
rectangular array of numbers
– The most basic way of storing data in MATLAB
– Others: structures and cell arrays
• Scalar = 1-by-1 matrix
• Vector = only one row or only one column
• Matrices are basically created using the ‘[ ]’
• Let’s create some
4
A typical 3-D array
5
Matrices 1: create Albert Durer’s
matrix
6
Constructing a Simple Matrix
• The simplest way to create a matrix in MATLAB is to
use the matrix constructor operator, [].
• Create a row in the matrix by entering elements
within the brackets.
• Separate each element with a comma or space:
row = [E1, E2, ..., Em]
row = [E1 E2 ... Em]
• For example, to create a one row matrix of five
elements, type
A = [12 62 93 -8 22];
7
Constructing a Simple Matrix
• To start a new row, terminate the current row
with a semicolon:
• A = [row1; row2; ...; rown]
• An example of 4-by-4 matrix of numbers:
A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
8
Specialized Matrix Functions
• zeros
• ones
• rand
• magic
• eye
• diag
• pascal
usage1: func(dim)
usage2: func(row,col)
9
Your turn:
find out the results of these
Ensure the
• zeros(3) + 5 parenthesis
are
complete!!!
• round(8*(rand(6)))
• diag(magic(5))
• diag(diag(magic(5)))
• eye(3)
• sum(magic(4))
• sum(magic(4),2)
10
Other Specialized Matrices
• compan • rosser
• gallery • magic
• hadamard • toeplitz
• hankel • vander
• hilb • wilkinson
• invhilb • pascal
•Data Types I
•Exercises
12
Variables and expressions
• MATLAB language provides mathematical expressions
– Entirely matrices
• MATLAB does not require any type declarations or
dimension statements.
– Variables are created appropriately as assigned
• Variable names consist of a letter, followed by any
number of letters, digits, or underscores.
– Case sensitive
• Expression – a sequence of variables and operators
13
Variables and expressions
• Expressions use familiar arithmetic operators and
precedence rules:
+ Addition
- Subtraction
* Multiplication
/ Division
\ Left division
^ Power
‘ Complex conjugate transpose
( ) Specify evaluation order
y = x^2 + 3*x + 1
F = 5*ones(3,3)
14
Do these...
1. 5+2
6. 6.3 -
2. 5*2 2.1045
3. 5/2 7. 3.6^2
4. 3 + 2 * 14 - 8. 1 + 2^2
32
9. sqrt(5)
5. 2.54 * 8>2.6
10. cos(pi)
15
Moving ahead ...
Numbers and Arrays (Matrices I)
Variables and expressions
Operators and Functions
•Input and Output
•Data Types I
•Script m-files
•Exercises
16
Operators and Functions
• Operators operate on data and variables
– Arithmetic +, -, *, /, etc
– Relational <, >, ==, ~=, >=, <=
– Logical &,|, &&, ||
• Functions carry out tasks or operate on the
supplied arguments
– Mathematical functions: sin, log, trace, etc
– I/O functions: load, save
– Utility functions clear, who
17
Operator Precedence
Precedence levels determine the order in which MATLAB evaluates an
expression
18
Operators and Functions
MATLAB has extensive library of functions for
mathematical and other computational purposes
- The power of MATLAB lies here
•Trigonometric functions
•Logarithmic functions
•Polynomial functions
•Matrices manipulation functions
•Data import and export functions
19
Built-in MATLAB functions
• Trigonometric Functions
sin, cos, tan, sec, asin, acos, atan,
sind, cosd, tand, asind, acosd, atand,
sinh, etc
• Logarithmic Functions
log, log10, log2, exp
sqrt, nthroot, etc
• Complex number functions
abs, angle, complex, real, imag, etc
20
Built-in MATLAB functions
• Polynomial functions
conv, poly, roots, polyfit, polyval
• Matrices analysis and manipulation functions
diag, eye, sum, max, min,
size, cat, reshape,
det, rank, trace, eig, etc
• Rounding and Remainder functions
round, Ceil, floor, fix, rem, mod
21
Try these now
For more:
Visit the Help Documentation
23
Moving ahead ...
Numbers and Arrays (Matrices I)
Variables and expressions
Operators and Functions
Input and Output
•Data Types I
•Script m-files
•Exercises
24
Input and Output 1
(saving your work)
• Save, load and recording functions
save(‘filename’, ’content’);
load(‘filename’)
diary(‘filename’)
• Displaying and writing functions
disp(varname); disp(expression);
str = sprintf(‘format’, A, ...)
fprintf(fileID, format, A, ...)
25
Moving ahead ...
Numbers and Arrays (Matrices I)
Variables and expressions
Operators and Functions
Input and Output
Data Types I
•Script m-files
•Exercises
26
Data Types I
So far we’ve been dealing with numbers
(numeric data type).
Other forms of data include:
• Characters and strings
• Logical class
27
Characters and strings
• Creating character arrays
hchar = 'h'; % a single character (1x1 array)
hstr = 'Hello World';
% a string of characters (1x11 vector)
% in a matrix of characters, the array must be
rectangular
name1 = ['Thomas R. Lee'; ...
'Sr. Developer'; ...
'SFTware Corp.'];
% else pad the shorter strings with space characters
name2 = ['Harold A. Jorgensen '; ...
'Assistant Project Manager'; ...
'SFTware Corp. '];
28
Characters and strings
• Compare two strings
C = strcmp(str1,str2)
C = strncmp(str1, str2, n)
• Converting to strings
char([72 104])
num2str(number)
29
Logical class
• represents a logical true or false state using
the numbers 1 and 0, respectively
• The values returned by relational and logical
operators and expressions
• It can be an array
[30 40 50 60 70] > 40
• Used in:
– Conditional statements, logical indexing, while
loops
30
Logical class
• The popular logic table
true | false
high | low
1 | 0
yes | no
•Exercises
32
Script m-files
• Typically a “source code” file, created using
the MATLAB editor/debugger.
• create and save code in files called M-files.
• can be reused anytime you wish to repeat
your calculations.
• Save files in your working folder
• Use valid MATLAB variable names
33
Script m-files
• Script -> the simplest type of MATLAB program
• Saved in m-file (i.e. with .m extension)
• Contains multiple sequential lines of MATLAB
commands and function calls
• Run script by typing name at the command line
35
Creating a Script m-file
• Type in the MATLAB commands, expressions and
function calls
• Good practice: one expression per line
• End each expression with semicolon as
appropriate
• Good practice: comment generously
• Good practice: present an easily
readable code
36
Sample Script m-file
>> edit plotRand
37
Execute Sample Script m-file
38
Output from sample script file
40
Your turn: create, and execute this one.
Examine your results
41
After this session ...
Numbers and Arrays (Matrices I)
Variables and expressions
Operators and Functions
Input and Output
Data Types I
Script m-files
Exercises
42
Exercises
• Listen attentively
• Endeavour to read very well
• Practice, Practice, Practice
• Feel free to experiment
43
Then go scratch out
some calculations
44