01-Intro_to_MATLAB
01-Intro_to_MATLAB
Lecture 1: Introduction
• Course Information
• Overview of Matlab
• Basic Syntax
• Canvas page:
https://canvas.sfu.ca/courses/89149
Materials will be posted online
• Matlab Online:
• https://www.mathworks.com/products/matlab-online.html
• Guidelines on collaborations:
https://www.haverford.edu/sites/default/files/Department/Math/Guidelines-for-
Homework-Collaboration.pdf
It is ok to discuss the assignments with others
Do not read other people’s solutions. Write your own answers and codes.
Do not become an inactive participant
Acknowledge your collaborations in your submission
• Second-time offender will fail the course, and receive the FD grade – Failed for
academic Dishonesty.
• https://www.sfu.ca/students/calendar/2025/spring/fees-and-
regulations/grading-policy/grading-systems-and-policies.html
3 Plotting
5 Combinatorics
8 Complex Numbers
10 Symbolic Computations
11 Numerical Differentiation and Integration
12 Simulink, Compiler, and Review for Exam
Main References
• Physical Modeling in MATLAB, Version 4.0, Allen B. Downey, Green Tea Press, 2021.
• Supporting MATLAB code for the book are in this GitHub repository.
• Experiments with MATLAB, Cleve Moler, 2011.
• Course Information
• Overview of Matlab
• Basic Syntax
L = 160 * membrane(1,100);
f = figure;
ax = axes;
s = surface(L);
s.EdgeColor = 'none';
view(3)
https://www.mathworks.com/help/matlab/visualize/creating-the-matlab-logo.html
https://www.mathworks.com/discovery/simulation-software.html
https://electrosome.com/am-generation-simulink/
ENSC 180 – Introduction to Engineering Analysis Tools 13
Simulation Tools
https://www.mathworks.com/products/stateflow.html
https://www.mathworks.com/products/stateflow.html
https://www.mathworks.com/help/simscape/
https://twitter.com/MATLAB/status/1290603083727286274
https://www.youtube.com/watch?v=jI0eWIgXsT4
Example: Use Symbolic Math Toolbox to find the 1st and 2nd derivative of f(x)
https://www.mathworks.com/help/symbolic/differentiation-featured.html
% declare symbolic variables
syms x
assume(x, 'real’)
f = (3*x^3 + 17*x^2 + 6*x + 1)/(2*x^3 - x + 3)
g = diff(f, x)
h = diff(f, x, 2)
https://www.mathworks.com/videos/what-is-deep-learning-toolbox--1535667599631.html
• help ***: will display the help info of the function in the command window
• doc ***: will open the Help Center window and display more extensive help
>> help sin
sin Sine of argument in radians.
sin(X) is the sine of the elements of X.
• Course Information
• Overview of Matlab
• Basic Syntax
Workspace
Window
Command Window
• All examples in this presentation make use of the MATLAB command window.
• Variable: A named value.
>> a = 10;
>>
• Row Vector: To put data in a row vector, type values within square brackets that
are separated by commas or spaces:
>> b = [3, 8, 4, 1, 2]
b =
3 8 4 1 2
• Column Vector: To put data in a column vector, type values within square brackets
that are separated by semicolons. The semicolon represents the end of a row.
>> c = [2; 9; 5]
c =
2
9
5
Transpose operator: ‘ (the prime or single quote symbol)
c = [2, 9, 5]’ will create the same column vector as above
https://www.mathworks.com/help/matlab/matlab_prog/fundamental-matlab-classes.html
• To create a matrix, just combine the row and column vector syntax.
• Use commas or spaces to fill in the rows.
• Use a semi-colon to end the row and start a new one.
>> A = [1 2 3; 4 5 6; 7 8 9; 10 11 12]
A =
1 2 3
4 5 6
7 8 9
10 11 12
• Concatenation works with all the array creation syntax taught thus far:
>> C = [1 2; 3 4]; F =
>> D = [9 9; 9 9]; 1 2 9 9
>> E = 5:8; 3 4 9 9
>> F = [C D; E; -2:1]; 5 6 7 8
-2 -1 0 1
p
n p
>> A = [1 2 3; 4 5 6] m =m
A =
n
1 2 3
4 5 6
>> A * B
>> B = [2 2 10; 10 10 10] Error using *
Incorrect dimensions for matrix multiplication. Check
B = that the number of columns in the first matrix matches
2 2 10 the number of rows in the second matrix. To operate on
10 10 10 each element of the matrix individually, use TIMES
(.*) for elementwise multiplication.
>> A' * B
2 3
ans =
3
3 2 = 3
42 42 50
54 54 70
66 66 90
• There are various built-in functions that are useful for creating arrays and matrices:
• Built-in functions generally run faster than manual implementations
• ones() and zeros() – Array of all ones and all zeros, respectively.
• eye() – Identity matrix.
• diag() – Diagonal matrices and diagonals of a matrix.
• true() and false() – logical 1 and logical 0 matrices, respectively.
• linspace() – create linearly spaced vectors.
• ndgrid() – rectangular grid in N-D space.
• [x1,x2,x3] = ndgrid(-2:.2:2, -2:.25:2, -2:.16:2);
•rand() – Uniformly distributed pseudorandom numbers in (0, 1)
• randn() – Normally distributed pseudorandom numbers: zero mean, unit
variance
• Standard math functions such as sin, cos, and sqrt are readily available and
operate element-wise on any matrix:
• Many functions will have outputs that differ in size or type from the inputs:
>> A = [1 2 3; 4 5 6];
>> [num_row, num_col] = size(A)
num_row = num_col =
2 3
>> A = magic(4)
A =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
>> A(4,2)
ans =
14
• This can also be used to manipulate data within the matrix, e.g.: set matrix A, rows
1 to 3, columns 3 to 4, equal to 0:
>> A(2:2:4,:) = []
A = 16 2 0 0
9 7 0 0
• Matlab stores arrays in memory as a big single column vector, made up of all
columns of A appended one after the other.
• 2: Linear Indexing: Therefore elements of an array can also be accessed by a
single index, regardless of the size or dimensions of the array.
https://www.mathworks.com/help/matlab/math/array-
indexing.html?searchHighlight=linear%20index&s_tid=srchtitle_support_results_1_linear%20index
• Any matrix or row vector can be transformed into a column vector using the colon
operator in round brackets:
What is the output of sum(A)? A row vector with the sum over each column.
>> sum(A)
ans =
5 5
• In the following example, b is a logical array that shows which values of a are
less than or equal to 5.
>> a = [1 8 4 5 2 -2 9];
>> b = a <= 5
b =
1 0 1 1 1 1 0
>> c = a > 0
c =
1 1 1 1 1 0 1
>> d = b & c
d =
1 0 1 1 1 0 0
• Note: Cannot use b && c here, because b and c are arrays, not scalar.
• 3: Logical Indexing: We can use the logical operator result to extract these values
correspond to value 1 in the logical result, or modify them.
>> isnan(x)
ans =
1 0 0 0 1