MATLAB SYNTAX | 1
MODULE 1
CLEAR WORKSPACE & COMMAND WINDOW
Remove variable from workspace clear <var_name>
Remove all variables from the workspace clear
Clear all commands and outputs in the command window clc
MATHEMATICAL FUNCTIONS
Scalar Operations + - * / ^
Trigonometric Functions (radians) sin(rad) cos(rad) …
Trigonometric Functions (degrees) sind(deg) cosd(deg) …
Square Root sqrt(x)
Nth Rooth nthroot(x,n)
Exponential exp(x)
Absolute Value abs(x)
Natural Log log(x)
Base 10 Log log10(x)
ROUNDING DECIMALS
Round to the nearest integer round(x)
Round to the nearest n decimal places round(x,n)
Truncate to the nearest integer fix(x)
DATA FORMATS
Fixed-point with 4 decimal digits format short
Fixed-point with 15 decimal digits format long
Scientific notation with 4 decimal digits format short e
Scientific notation with 15 decimal digits format long e
Two decimal digits format bank
MATLAB SYNTAX | 2
ARRAYS
Size (m x n) <row> x <column>
Row Vector (1 x n) <var_name> = [ 1 2 3 4 5 ]
Column Vector (m x 1) <var_name> = [ 1 ; 2 ; 3 ; 4 ; 5 ]
EQUALLY-SPACED VECTORS
With specified spacing <var_name> = start : step : stop
With specified number of elements <var_name> = linspace(start , end , no.)
INDEXING
Specific element <var_name> (row , column)
All elements in specific column/s (all rows) <var_name> ( : , [col1 col2] )
All elements in specific row/s (all columns) <var_name> ( [row1 row2] , : )
Specific row and column (intersection) <var_name> ( [row1 row2] , [col1 col2] )
CONCATENATION
Vertical (same no. of columns) <var_name> = [ matrix1 ; matrix2 ]
Horizontal (same no. of rows) <var_name> = [ matrix1 matrix2 ]
MATLAB SYNTAX | 3
IDENTITY MATRIX
Main diagonal elements are equal to one(1) and zero(0) elsewhere
Square matrix eye ( n )
Specified row and column eye ( m , n ) eye ( [m , n] )
Same size as another matrix eye ( size(<var_name>) )
ONES MATRIX
All elements equal to one(1)
Square matrix ones ( n )
Specified row and column ones ( m , n ) ones ( [m , n] )
Same size as another matrix ones ( size(<var_name>) )
ZERO MATRIX
All elements equal to zero(0)
Square matrix zeros ( n )
Specified row and column zeros ( m , n ) zeros ( [m , n] )
Same size as another matrix zeros ( size(<var_name>) )
MAGIC SQUARE
Equal sum of rows and columns
Square matrix from integers 1 through n2
<var_name> = magic (n)
n≥3
- DISTRIBUTED PSEUDORANDOM NUMBERS
UNIFORM NORMAL
Square matrix rand ( n ) randn ( n )
rand ( m , n ) randn ( m , n )
Specified row and column
rand ( [m , n] ) randn ( [m , n] )
Same size as the other rand ( size(<var_name>) ) randn ( size(<var_name>) )
Scalar rand randn
MATLAB SYNTAX | 4
DIAGONAL
Elements in the diagonal (square matrix)
<var_name> = diag (<vector>)
Elements of vector on the main diagonal
<var_name> = diag ( [1 2 3 4 5 6 7 8] )
Specifies placement of elements on diagonal
<var_name> = diag (<vector> , k)
k=0 : main k > 0 : above k < 0 : below
Returns elements from a matrix
Column vector of main diagonal elements
<var_name> = diag (<matrix>)
Column vector of specific diagonal elements
<var_name> = diag (<matrix> , k)
TRIANGULAR MATRIX
All elements above or below the main diagonal are zero(0)
Lower Triangular Matrix
Lower triangle (above zeros) tril (<var_name>)
Specify diagonal
tril (<var_name> , k)
k=0 : main k > 0 : above k < 0 : below
Upper Triangular Matrix
Upper triangle (below zeros) triu (<var_name>)
Specify diagonal
triu (<var_name> , k)
k=0 : main k > 0 : above k < 0 : below
MATLAB SYNTAX | 5
MATRIX OPERATIONS
Addition A+B Same matrix size
Scalar Multiplication k*A
Multiplication A*B Equal inner dimensions
Exponentiation A*A A^n
Division A * inv(B) A/B
Transpose A‘
Inverse inv (A)
Determinant det (A)
ELEMENT-WISE OPERATIONS
Addition A+B
Multiplication A .* B
Division A ./ B
Exponentiation A .^ n
MATLAB SYNTAX | 6
POLYNOMIAL OPERATIONS
Declaration f = [ an an-1 an-2 … a1 a0 ]
Addition / Subtraction A+B
Multiplication conv ( A , B )
Division [ q , r ] = deconv (num , den)
(Answer: q + r / den)
Roots roots (A)
Polynomial Given the Roots poly (A_roots)
Value of Polynomial polyval ( A , x )
Partial Fraction Expansion [ r , p , k ] = residue (num , den)
(Answer: r1/(s-p1) + … + rn/(s-pn) + k(s)
DIFFERENTIATION
MATLAB BUILT-IN ( polyder )
Derivative of p polyder (p)
Derivative of ab polyder (a , b)
Derivative of a / b; [ q , p ] = polyder (a , b)
Returns numerator q and denominator p
SYMBOLIC MATH ( diff )
Differentiates f with respect to x, n times diff ( f , ‘x’ , n )
INTEGRATION
MATLAB BUILT-IN ( polyint )
Integral of p ; with constant of integration k=0 polyint (p)
Integral of p ; with specific constant k polyint (p , k)
SYMBOLIC MATH ( int )
Integral of f with respect to x from a to b int ( f , x , a , b )
MATLAB SYNTAX | 7