CCS 202: COMPUTATIONAL
MATHEMATICS WITH
MATLAB
(3 CREDITS)
BY
AVEYOM VALENTINE
MATLAB
•MATLAB (short for MATrix LABoratory)
•Is a state of the art mathematical software package, which is
used extensively in both academia and industry.
•It is interactive for numerical computation and data
visualization, which along with its programming capabilities
provide a very useful tool for almost all areas of sciences and
engineering.
USES OF MATLAB
• Data Analysis and Visualization: MATLAB excels in analyzing, cleaning, and
visualizing complex data sets from various fields like climatology, predictive
maintenance, and finance. It offers tools for importing, cleaning, and visualizing data in
various formats like graphs and charts.
• Algorithm Development: MATLAB is used for developing and testing algorithms,
including those related to machine learning, deep learning, and image processing.
• Modeling and Simulation: It's a fundamental tool for modeling and simulating various
systems and processes, including those in engineering, science, and control systems.
USES OF MATLAB
• Application Development: MATLAB can be used to develop applications, including
graphical user interfaces (GUIs), embedded systems, and Internet of Things (IoT)
applications.
• Education and Learning: MATLAB is used in introductory and advanced courses in
mathematics, engineering, and science.
• Image and Signal Processing: MATLAB supports image and signal processing tasks like
image enhancement, filtering, and object recognition.
• Control Systems Engineering: MATLAB provides tools for analyzing and designing
control systems.
• Robotics and Computer Vision: MATLAB is used for robotic simulations, computer
vision tasks, and autonomous navigation.
THE BASICS OF MATLAB
The User Interface
•Matlab’s user interface is partitioned into different
sub-windows. The most important parts of the user interface
are:
•The command window
•The workspace
•The current directory browser
Command history: a list of instructions
executed by MATLAB is shown here.
Workspace: shows a list of
variables created by MATLAB.
As you can see, the value of ‘aaa’
is shown.
If you want to see help,
you can type help at the
command window.
Or you can press F1
to display the help
window. Click on
Open Help Browser
to search for a
specific function.
ASSIGNMENT
1. Recall that, when adding fractions, the smallest common
denominator (smd) must be determined. For example, the smd
of and is 20.
Use MATLAB Help Window to find a MATLAB built-in
function that determines the least common multiplier (lcm)of
two numbers. Then use the function to show that the lcm of :
a) 4 and 10 is 20 b) 6 and 38 is 114
MAKING FOLDERS
•It is important to use folders to keep your programs
organized
•Click the ‘Make New Folder’ button, and change the name
of the folder. Do NOT use spaces in folder names.
•The current directory is now the folder you just created
DISPLAY FORMATS
BUILT-IN FUNCTIONS
BUILT-IN FUNCTIONS (CONT.)-TRIG
FUNCTIONS
BUILT-IN FUNCTIONS
(CONT.)-ROUNDING FUNCTIONS
VARIABLES
•Data are stored in Matlab’s working memory as variables
•A variable is a reserved(place) in computer’s memory that
can be referenced with a unique name.
•A variable can contain various kinds of data. This means that
a variable is of a certain data type. Examples of data types:
simple numbers, matrices, character sequences (strings).
VARIABLES CONT.
•Variables are broadly classified in MATLAB as scalars, vectors,
and arrays.
VARIABLE NAMING CONVENTIONS
•A variable is identified by a unique name. the name
has to begin with a letter ( upper or lower case), after
that it can contain further letters, numbers, or
underscore. E.g., X12, rate_const, Flow_rate
•Variable names are case sensitive.
E.g., FLOW, flow, Flow, FlOw are all different
variables
THE ASSIGNMENT OPERATOR
THE BASICS OF MATLAB CONT’
•To begin with, Type any operation on two or more number in
the command window ;
•E.g., >> 7 + 8 <CR>
ans =
15
The answer to the typed command is given the name ans.
The ans is now a variable that we can use again
THE BASICS OF MATLAB
•Example,
>> ans * 20
ans =
300
Note
•that MATLAB has updated the value of ans
•Spacing of operators in formulas do not matter
e.g., 6+4*5-6/3*6 = 6 + 4 * 5 – 6 / 3 * 6
BASIC MATHEMATICAL COMPUTATIONS
•Operations
•An m-file environment has all the standard arithmetic
operations(addition, subtraction etc) and functions (sine,
cosine, logarithm etc)
•Given that X and Y are scalars:
SOME COMMON MATHEMATICAL
OPERATIONS
Operation M-file
X+Y X+Y
X-Y X-Y
XY X*Y
X/Y X/Y
X ^y
exp(x)
log10(x)
ln(x) log(x)
Log2(x)
Cos(x) Cos(x)
Sin(x) Sin(x)
EXPRESSIONS
•Expressions are formed from numbers, variables and operations
•The operations have different precedence.
Recall: BODMAS or BEDMAS
EXPRESSIONS CONT’D
Expression
• Examples MATLAB Computed
Expression value
5^2+6^2 61
(5 + 6)^2 121
(2 + 3) / (4 - 5) -5
log10(100) 2
log(4 * (2 + 3)) 2.9957
EXPRESSIONS CONT’D
Note:
•A semicolon added at the end of a line of expression
suppresses the output
•To split a statement a cross multiple lines, enter three periods
… at the end of the line to indicate that it continues on the
next line. E.g.,
(6 +9)…
/(4 - 6)
SUB MATRICES
•
CHANGING VALUES OF MATRIX ELEMENTS
• For example,
>> A =[11 12 13 14; 21 22 23 24; 31 32 33 34]
A=
We can change the element in row 1 column 1
to -34 as follows:
>> A (1, 1)= -34
CREATING EMPTY ARRAYS
• This is deleting a row or a column in a matrix.
Example, considering A, let us delete the
second row:
>> A (2,:)= [ ]
•A =
-34 12 13 14
31 32 33 34
CREATING NEW MATRICES FROM ROW AND
COLUMN REFERENCING
•• E.g., Given a matrix A, we can copy the 2nd row
four times as follows:
>>A = [1 2 3; 4 5 6; 7 8 9];
>>B = A([2 2 2 2], :)
BUILT-IN FUNCTIONS
•
BUILT-IN FUNCTIONS CONT’D
•
BUILT-IN FUNCTIONS CONT’D
• Vector Functions: these operate essentially on vectors returning a scalar value,
they include:
• max: largest component
• Min: smallest component
• Length: length of a vector
• Sort: sort in ascending order
• Sum: sum of elements
• Prod: product of elements
• Median: median value
BUILT-IN FUNCTIONS CONT’D
• Matrix Functions: Matlab’s power comes from its matrix functions:
• Eye: identity matrix
• Zeros: matrix of zeros
• Ones: matrix of ones
• Diag: extract diagonal of a matrix or create diagonal matrices
• Triu: upper triangular part of a matrix
• Tril: lower triangular part of a matrix
• Rand: randomly generated matrix
BUILT-IN FUNCTIONS CONT’D
• Size: size of a matrix
• Det: determinant of a square matrix
• Inv: inverse of a matrix
• Rank: rank of a matrix
• Rref: reduced row echelon form
• Eig: eigenvalues and eigenvectors
• Poly: characteristic polynomial
• Norm: norm of matrix
• Lu: LU factorization of decomposition
• Qr: QRfactorization
• Chol: Cholesky decompositon
VECTORS
These are in two forms; row vectors and column vectors.
The number of entries is known as the “length” of the vector and the
entries are often referred to as “elements” or “components” of the
vector.
The entries must be enclosed in square brackets
Row Vectors are lists of numbers separated by either commas or
spaces.
e.g. >> v = [1 3 sqrt(5)]
v=
1.0000 3.0000 2.2361
>> length(v)
ans=
3
VECTORS CONT’D
The operations are carried out elementwise
We can also build row vectors from existing ones:
>> w = [1 2 3]; z = [8 9];
>> t = [2*z, w], sort(t)
t=
16 18 1 2 3
ans=
2 3 16 18
>> w(2) = -2, w(3) ?????
COLON NOTATION
This is a shortcut for producing row vectors:
>> 1:4, 3:7, 1:-1
ans =
2 3 4
ans =
4 5 6 7
ans =
Empty matrix: 1-by-0
In general, a : b : c producesa vectorof entries startingwiththe value a,
incrementing by the value b until it gets to c
e.g. perform 0.32:0.1:0.6, -1.4:-0.3:-2
TRANSPOSING VECTORS
We can convert a row vector into a column vector (vice versa) by a process called
transposing –denoted by ‘.
>> w, w', c, c'
w =1 -2 3
ans = 1
-2
c= 3
1.0000 3.0000
ans = 2.2361
1.0000 3.0000 2.2361
Perform t = w + 2*c’, T = 5*w’-2*c
TRANSPOSING VECTORS CONT’D
If x is a complex vector, then x’ gives the complex conjugate transpose of x
>> x = [1+3i, 2-2i]
x = 1.0000 + 3.0000i 2.0000 -2.0000i
>> x'
ans =
1.0000 -3.0000i
2.0000 + 2.0000i
• Reminder: Matlab is case sensitive!
KEEPING A RECORD
Issuing the command
>> diary mysession
will cause all subsequent text that appears on the screen to be
saved to the file mysession located in the directory in which
Matlab was invoked.
You can use any legal filename except the names on and off.
The record my be terminated by
>> diary off
The file mysession may be edited with text editors to remove .
KEEPING A RECORD CONT’D
If you wish to quit Matlab midway through a calculation so as to continue at a
later stage:
>> save thissession
will save the current values of all variables to a file called
thissession.mat. This file cannot be edited.
When you next startup Matlab, type
>> load thissession
and the computation can be resumed where you left off.
A list of variables used in the current session may be seen with
the commands who or whos