Documente Academic
Documente Profesional
Documente Cultură
Basic M ATLAB
Isaac Ye
UOIT
MATH 2070U
InteractiveM ATLAB Numeric data logical operators Other data types
Basic M ATLAB
Numeric data
Numeric arrays
Matrix and array operators
Boolean/logical operators
Key questions
Arithmetic operators
Order of operations
Order of operations
42 −145
3
1-5*4^2/3+3/2 = 1 − 5 × + =
3 2 6
' −24.16666667
2
3 √
3 3
1-5*4^(2/3)+3/2 = 1 − 5 × 4 3 + = 1 − 5 42 +
2 2
' −10.09921050
InteractiveM ATLAB Numeric data logical operators Other data types
Identifiers in M ATLAB
Reserved words
• In every programming language, some names should not
or cannot be used as identifiers since they are already
reserved for some other purpose; these names are
keywords or reserved words
• e.g., identifier pi in M ATLAB reserved for
π ' 3.1415926535897932
• e.g., identifier tan in M ATLAB reserved for tangent
function
• Other reserved words in M ATLAB: for, while, if, else,
end, etc.
• Warning: Some programming languages (including
M ATLAB) let you redefine identifiers associated with
reserved words. Avoid doing this unless you have a good
reason and you know what you are doing!
InteractiveM ATLAB Numeric data logical operators Other data types
Moral:
If you don’t remember the exact identifier for a given function
or constant or feature in a given programming language, make
sure you know how to look it up! In interpretive programming
languages, this means that online help is your friend!
2x + cos2 y
(2*x + cos(y)^2) / (3*x + 5*sin(y)^2)
3x + 5 sin2 y
p p
1 + 5φ − 1 + 2p
( sqrt( 1+5*phi ) - sqrt( 1+2*p ) ) / p
p
lookfor very useful when you don’t know the name of the
command but you have pertinent keywords
InteractiveM ATLAB Numeric data logical operators Other data types
Warning:
format affects output to screen only!
Internal representation in M ATLAB is different
InteractiveM ATLAB Numeric data logical operators Other data types
Arrays
Warning:
• ; (semicolon) separates rows within arrays (i.e., between [ ])
• , (comma) separates columns within arrays (i.e., between [ ])
• Outside arrays, semicolons & commas separate statements
• Infer correct meaning of symbols from context (help punct)
InteractiveM ATLAB Numeric data logical operators Other data types
>> A = [ 1 2 3 4; 5 6 7 8; 9 10 11 12 ]
>> A(2,1) % returns 5
>> A(end, end-2) % returns 10
>> A(:) % Outputs entries of A as one-dimensional vector
>> A(7) % Makes sense even though A is 2D array
Warning:
• ( ) (parentheses) used for array indexing and function invocation
• Infer correct meaning of symbols from context
InteractiveM ATLAB Numeric data logical operators Other data types
Colon notation
• Colon notation is used to specify ranges of integer values
• Colon notation can specify larger steps
• Colon notation can specify ranges nonpositive steps
• Colon notation can specify non-integer ranges
• Colon notation can be used for indexing arrays
Execute the following statements & explain
Conformable arrays
Matrix algebra
Scalar expansion
• Scalar expansion in M ATLAB: mixed operations with
scalars and arrays lead to “expanding” scalar to
conformable array
• Scalar expansion inconsistent with conventions of linear
algebra
• Scalar expansion avoids unnecessary loops
Execute the following statements & explain
>> A = [ 1 2 3 ; 4 5 6 ];
>> A + 3.1 % Scalar expansion of sum
>> A.^2 % Scalar expansion of exponent
>> 2.^A % Scalar expansion of base
InteractiveM ATLAB Numeric data logical operators Other data types
Vectorised operations
Logical operators
• Logical or boolean data: true (1) or false (0)
• Logical operators and truth tables
Syntax Meaning
and(a,b) a&b AND
or(a,b) a|b OR
not(a) ~a NOT
xor(a,b) XOR
and 0 1 or 0 1
0 0 0 0 0 1
1 0 1 1 1 1
not xor 0 1
0 1 0 0 1
1 0 1 1 0
InteractiveM ATLAB Numeric data logical operators Other data types
Syntax Meaning
> (strictly) greater than
< (strictly) less than
>= greater than or equal to
<= less than or equal to
== equal to
~= not equal to
• Relational operators compare two numeric quantities
• Assignment operator = distinct from comparison ==
• (More on logical variables when looking at control flow)
InteractiveM ATLAB Numeric data logical operators Other data types
Assignment operator
>> A = -3:2, B = 2 - A
>> A > -2 % evaluates to [ 0 0 1 1 1 1 ]
>> B + 2*A > 1
>> C = abs(A);
>> test = ( C == A ) % evaluates to [ 0 0 0 1 1 1 ]
>> x = linspace(0,3,7)
>> condition1 = (x>=1) & (x<2)
>> condition2 = (x<=1) | (x>=2)
InteractiveM ATLAB Numeric data logical operators Other data types
Polynomials in M ATLAB
• Represented as (row) vector of coefficients of monomials
f = [5 -3 2] represents f (x) = 5x2 − 3x + 2
g = [7 2 4 -6.1] represents g(x) = 7x3 + 2x2 + 4x − 6.1
h = [4 -1 0 0] represents h(x) = 4x3 − x2
• Missing monomial terms have coefficient zero
g = [ 1 0 -3 0 0 -1 ] represents g(x) = x5 − 3x3 − 1
because g(x) = x5 + 0 x4 − 3x3 + 0 x2 + 0 x − 1
Summary
• Basics of interactive computation in M ATLAB
• Arithmetic operators and order of operations
• Identifiers and reserved words
• Using mathematical functions in M ATLAB
• Documentation: help, doc, and lookfor
• Controlling/preserving output: format and diary
• Numeric variables: real and complex numbers
• Arrays: input, indexing, colon notation and operators
• standard matrix algebra operations
• vectorised operations
• scalar expansion
• Logical variables: relational and logical operators
• Other data types: char/strings, polynomials, structures,
cells