Principles of MATLAB Programming slides
Principles of MATLAB Programming slides
Soroush Mirmobini
December 2024
Channel Contact
https://t.me/soroushmirmobini
[email protected]
https://t.me/sm_bme
Reference
https://www.elsevier.com/books-and-journals/book-companion/9780323857734
Table of Contents
Control
Text and Files Visualization Functions
Structures
Solving Supplementary
Statistics End
Equations Topics
1. Introduction to
“Computer Programming”
5
1. Preface
• MATLAB Environment
1. Preface 6
Introduction to Computer Programming
• What Is a Computer?
• History of Computer
1. Preface 7
Decimal Binary Octal Hexadecimal
Number Bases (Base 10) (Base 2) (Base 8) (Base 16)
0 0000 0 0
1 0001 1 1
2 0010 2 2
3 0011 3 3
4 0100 4 4
5 0101 5 5
6 0110 6 6
7 0111 7 7
8 1000 10 8
9 1001 11 9
10 1010 12 A
11 1011 13 B
12 1100 14 C
13 1101 15 D
14 1110 16 E
15 1111 17 F
16 10000 20 10
17 10001 21 11
8
1. Preface
Base 2
Boolean Operators
0/1
Table 1: “Not” Truth Table
A Buffer Not A ( ~ )
https://www.gsnetwork.com/digital-logic-gates /
0 0 1
• Buffer/Not
1 1 0
0 0 0 1 0 1 0 1
• Or/Nor 0 1 0 1 1 0 1 0
1 0 0 1 1 0 1 0
• X-Or/X-Nor 1 1 1 0 1 0 0 1
1. Preface 9
Introduction to Computer Programming
Write
Fetch Decode Execute
Back
1. Preface 10
1. Preface 11
Introduction to Computer Programming
• Back-End/Front-End/Full-Stack Developers
• Web Developers
Programmers
• Game Developers
• Embedded System Programmers
1. Preface 12
Start
Start
https://madhusona.github.io/GE8151/Unit-I/5-Flowchart
1. Definitions
• Algorithm
Some
Processes
2. Command 1 Output(s)
Condition(s) 1
3. Command 2
Output(s)
Condition(s)
. 1
• Flowchart .
Some Output(s)
. Some
Processes 2
Processes
n-1. command n-1
n. End
• Pseudo-code Output(s)
2
End
End
1. Preface 13
1. Preface 14
MATLAB Environment
• Workspace
• Command Window
• Command History
• Directories and Paths
• Home, Apps, Plots
• Editor, Publish, View
• Preferences
1. Preface 15
1. GitHub
2. Stack Overflow
3. Geeks for Geeks
4. Research Gate (Questions)
5. Linkedin
6. Reddit
7. Quora
8. … 1. Preface 16
2. Introduction
to
MATLAB
17
2. Introduction to MATLAB
• Variables
• Semicolon (;)
• Data Types and Data Precisions
• Converting Between Data Types
• Mathematical Operators and Functions
• Trigonometric & Hyperbolic Functions
• Scripts
2. Introduction to MATLAB 18
Variables
Naming Rules
• Case Sensitivity
• Signs
• Numbers
• Space
2. Introduction to MATLAB 19
;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;; Semicolon ;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;
2. Introduction to MATLAB 20
Uni-Type
0/1 (Numerical) Multi-Type
(true/false) Text Array Arrays
2. Introduction to MATLAB 21
Data Types and Data Precision
Numerical Data
Text Data
• Characters (char)
• Strings (str)
Functions
2. Introduction to MATLAB 22
Converting between Data Types
• char()
• double()
• single()
• int8() … int64()
• uint8() … uint64()
• logical()
2. Introduction to MATLAB 23
Operators
Arithmetic
Operators + - * / ^ \
Logical
Operators & && | || ~
Conditioning
(Comparing) == ~= > >= < <=
Operators
2. Introduction to MATLAB 24
https://www.mathworks.com/help/matlab/matlab_prog/operator-precedence.html
25
2. Introduction to MATLAB
MATLAB Operator Precedence
Mathematical Functions
Function Description
round (x) Round to the nearest integer
ceil (x) Round to the nearest integer toward “+ꚙ”
floor (x) Round to the nearest integer toward “-ꚙ”
fix (x) Round to the nearest integer toward “0”
mod (a,b) Remainder after division M = a-b*floor(a/b)
rem (a,b) Remainder after division M = a-b*fix(a/b)
․ log (x) ․ ln 𝑥
․ log10 (x) ․ log 𝑥
abs (x) Absolute Value 𝑥
2
sqrt (x) Square Root 𝑥
exp (x) Exponential 𝑒𝑥
factorial (x) Factorial 𝑥!
angle (x) Phase (angle) of a complex number
2. Introduction to MATLAB 26
Trigonometric Functions
2. Introduction to MATLAB 27
Trigonometric Functions
• Arcs:
2. Introduction to MATLAB 28
29
https://www.whitman.edu/mathematics/calculus_online/section04.11.html
https://medium.com/mathadam/beyond-soh-cah-toa-from-circular-to-hyperbolic-
trigonometry-a3125788c383
2. Introduction to MATLAB
Hyperbolic Functions
Hyperbolic Functions
𝑒 𝑥 −𝑒 −𝑥 𝑒 2𝑥 −1 1−𝑒 2𝑥
• sinh(𝑥) = 2
= 2𝑒 𝑥
= 2𝑒 −𝑥
𝑒 𝑥 +𝑒 −𝑥 𝑒 2𝑥 +1 1+𝑒 2𝑥
• cosh 𝑥 = 2 = 2𝑒 𝑥
= 2𝑒 −𝑥
2. Introduction to MATLAB 30
Scripts
Sections %% (Ctrl + Alt + Enter)
Smart Indent
Script Ctrl + I
Run F5
Terminate Ctrl + C
2. Introduction to MATLAB 31
3. Arrays
32
3. Arrays and Linear Algebra
• Linear Algebra
• Operators
• Functions
• An = A(n);
• A = [a1 ,a2 , a3 , … , an];
Columns • An1-n2 = A(n1 : n2);
• A = [a1 a2 a3 … an];
Addressing • Axy = A( [nx , ny , …] );
• add = [nx , ny , …]; Aadd = A(add);
• Aend = A(end);
• A = [a1 ; a2 ; a3 ; … ; an];
Creating
• A = [a1
Rows a2 • B = transpose (A);
a3 Transposing
... • B = A’;
an];
, ՞ 𝑆𝑝𝑎𝑐𝑒 • A = [… ]3𝑥3
• 𝑎22 = A (5);
• perms, randperm
• Operators
.* ./ .^ * / \ ^ + -
• Functions
• Cell
• A = cell(size);
• Table
• T = table(Variables);
• Structure
• A = struct();
Value2/Variable2, …);
• A.a1 = x; A.a2 = y;
• X = A.a1; Xn = A.a1(n)
43
4. Control Structures
• for
• if
• switch
• While
• Supplementary Functions
4. Control Structures 44
for Loop Variable = An Array or Start:Step:End
for: Statement(s)
end
for loop will be executed as many columns as the loop variable has
4. Control Structures 45
if (Condition(s) #1)
Statement(s) 1
Statement(s) n
else
Default
Statement(s)
end
4. Control Structures 46
switch Conditioning Variable
case Value #1
Statement(s) 1
case Value #2
. Statement(s) 2
switch: . ..
. .
case Value #n
Statement(s) n
otherwise
Default
Statement(s)
end
4. Control Structures 47
while Condition(s)
while: Statement(s)
end
4. Control Structures 48
Supplementary Functions
• break
• continue
• pause ()
4. Control Structures 49
5. Visualization
50
5-1. 2-D Visualization
51
2-D Visualization - Basic Functions
54
3-D Visualization
• comet3
• plot3 • bar3
• surf, surfl, surfc • stem3 • shading
• mesh, meshc, meshz
• pie3 • view
• Meshgrid
• slice • hidden
• ezsurf
• ezmesh
• fill3 • camera
• ezplot3 • scatter3
• contour3
56
6. Text and Files
• Text
• Files
• Publish
• sprintf
• disp
• load, importdata
61
7. Functions
• Declaring Functions
• Calling Functions
• Debugging Codes
7. Functions 62
Declaring Functions
function [Output Variables] = Function Name (Input Variables)
end
File name should be exactly same as the function name: “function name.m”
7. Functions 63
Calling Functions
7. Functions 64
Input / Output Checking
• narginchk, nargoutchk
• varargin, varargout
• Variable Size
• Application
7. Functions 65
Debugging Codes
• Error
Break Points
7. Functions 66
8. Solving Equations
67
8. Solving Equations
8. Equation Solving 68
Symbolic and Polynomial Equations
8. Equation Solving 69
Linear Equation Systems
8. Equation Solving 70
Integration and Differential Equations
8. Equation Solving 71
9. Statistics
72
9. Statistics
9. Statistics 73
9. Statistics
Descriptive Statistics:
• Uni-variate
• Bi-variate
• Regression Analysis
• ROC Analysis
Inferential Analysis
• Basic Concepts
• Parametric Hypothesis Testing
• Non-Parametric Hypothesis Testing
• Multiple and Multivariate Hypothesis Testing
Experimental Design and Sample Size Calculations
9. Statistics 74
Descriptive Statistics
76
Descriptive Statistics - Basic Concepts
9. Statistics 77
Descriptive Statistics - Univariate Data Visualization
9. Statistics 78
Descriptive Statistics - Histogram
9. Statistics 79
Descriptive Statistics - Univariate Data Measures
Range range 𝑅𝑎𝑛𝑔𝑒 = 𝑚𝑎𝑥 𝑋 − 𝑚𝑖𝑛 𝑋 1) Sort the x in ascending or descending order
𝑛+1
𝑥 𝑛: 𝑜𝑑𝑑
2
Median median 2) 𝑚𝑒𝑑𝑖𝑎𝑛 = ൞ 𝑛 𝑛
1Τ
Data: Symmetric with no outliers 2 𝑥
2
+𝑥
2
+1 𝑛: 𝑒𝑣𝑒𝑛
n: number of elements exist in x
MATLAB
Operation Formula
Function
𝑛
1
Mean mean 𝜇 = 𝑥ҧ = 𝑥𝑖
𝑛 1) Sort the x in ascending or descending order
𝑖=1
2) 𝑚𝑒𝑑𝑖𝑎𝑛 =
𝑛
Standard 1 Inter-Quartile 𝑥
3
× 𝑛+1 −𝑥
1
𝑛+1× 𝑛: 𝑜𝑑𝑑
std 𝜎=𝑠= (𝑥𝑖 −𝑥)ҧ 2 iqr ቐ 4 4
Deviation 𝑛−1 Range 𝑠𝑎𝑚𝑒 𝑎𝑠 𝑚𝑒𝑑𝑖𝑎𝑛 𝑓𝑜𝑟 𝑒𝑎𝑐ℎ 𝑞𝑢𝑎𝑟𝑡𝑖𝑙𝑒 𝑛: 𝑒𝑣𝑒𝑛
𝑖=1
n: number of elements exist in x
𝑛
1
Variance var 𝜎2 = 𝑠2 = (𝑥𝑖 −𝑥)ҧ 2
𝑛−1
𝑖=1
9. Statistics 80
Descriptive Statistics - Bivariate Data Visualization
9. Statistics 81
Descriptive Statistics - Bivariate Data Measures
MATLAB
Operation Formula
Function
𝑛
1
Covariance cov 𝑐𝑜𝑣(𝑥, 𝑦) = (𝑥𝑖 − 𝑥)(𝑦
ҧ 𝑖 − 𝑦)
ത
𝑛−1
𝑖=1
𝑛
corr, (𝑥𝑖 − 𝑥)(𝑦
ҧ 𝑖 − 𝑦)
ത
Pearson’s Correlation 𝑟𝑥𝑦 =
corrcoef
𝑖=1
σ𝑛𝑖=1(𝑥𝑖 −𝑥)ҧ 2 σ𝑛𝑖=1(𝑦𝑖 −𝑦)
ത 2
9. Statistics 82
P-Value
9. Statistics 83
Descriptive Statistics - Regression Analysis
• polyfit
• polyval
9. Statistics 84
Descriptive Statistics – ROC Analysis
Positive Predictive
PPV = TP/(TP + FP)
Value (Precision)
Negative Predictive
NPV = TN/(TN + FN)
Value
Recall Re = TP/(TP+FN)
F1 Score F1 = 2TP/(2TP+FP+FN)
9. Statistics 85
Inferential Statistics
86
Inferential Statistics - Basic Concepts
• Probability
• Single Event
• Multiple Events
• Probability Distribution
• Normal Distribution
• The Central Limit Theorem
• Standard Error Of The Mean
• Confidence Intervals Of The Mean
9. Statistics 87
Inferential Statistics - Parametric Hypothesis Testing
• Hypothesis
• Null Hypothesis
• Alternative Hypothesis
• Type I Error
• Type II Error
• Hypothesis Testing
• One sample.
• Two-sample paired data.
• Two-sample unpaired data
• z-Test
9. Statistics 88
Inferential Statistics - Non-Parametric Hypothesis Testing
• Sign Test
• Mann–Whitney U Test
• Chi-Square Test
9. Statistics 89
Inferential Statistics - Multiple and Multivariate Hypothesis Testing
9. Statistics 90
Complementary Topics
91
Inferential Statistics - Experimental Design and Sample Size Calculations
• Experimental And Observational Studies
9. Statistics 92
Inferential Statistics - Statistical Shape Models (SSMs)
• Forming An SSM
• Parameterize the Shape
• Align the Centroids
• Compute the Mean Shape Vector
• Compute the Covariance Matrix
• Compute the Eigenvectors and Eigenvalues
9. Statistics 93
Supplementary Topics
94
Some Statistical Plots
• cdfplot • normplot
• ecdf • wblplot
• probplot • qqplot
9. Statistics 95
Inferential Statistics - Probability Distributions
9. Statistics 96
10. Supplementary Topics
97
10. Supplementary Topics - General
• Top-Down
• Program Designing Approaches: • Bottom-Up
• Combined
• Introduction to Simulink
• Introduction to GUI
• Fuzzy Concepts
• Optimization Algorithms
matlab
1-essentials-of-neuroscience-with-
atlabcentral/fileexchange/15438
https://www.mathworks.com/m
10. Supplementary Topics 101