Professional Documents
Culture Documents
Matlab Lecture 1
Matlab Lecture 1
Introduction to MATLAB
Math-306 : Programming & Numerical Methods Lecture # 1
Outline
(1)Getting Started
(2)Making Variables
(3)Manipulating Variables
(4)Basic Plotting
Math-306 : Programming & Numerical Methods Lecture # 1
•• 1@l«MATLAB Files\MATLAB\R2009b\toolbox\spider\functions\norm.m
has
C,-g,::,::,.
t)ants_,yde,m
Jr.»
potential
t)ants_ioformation.m
t)ants_prirflilrypla<;-,g.m
t)ants_tra<eupdomg.m
~bisection.asv Workspa
t:ibisectloo.m Command
~choles_al.xIs ce
1~:Current Window
€Ide
~:~Folder
Cleo
~Geopctyme"Dat"'rom5't'M.xIs
~.~.
iBhs_err_pi(lo\W(lJog (ommandHi<lory
€!N.wtonPooI ...
EBPCA·p\II:tingtoworb.>!It
EBx.~
'r~O
9attol
9attool
EBy,,..t
History
Math-306 : Programming & Numerical Methods Lecture # 1
Getting Started
Customization
File -> Preferences
Allows you personalize your MATLAB experience
Math-306 : Programming & Numerical Methods Lecture # 1
MATLAB Basics
MATLAB Basics
MATLAB Basics
MATLAB Basics
MATLAB Basics
MATLAB Basics
MATLAB Basics
MATLAB Basics
who
MATLAB replies with the variables in your workspace
Math-306 : Programming & Numerical Methods Lecture # 1
who
MATLAB replies with the variables in your workspace
what
MATLAB replies with the current directory and MATLAB …les in the
directory
Math-306 : Programming & Numerical Methods Lecture # 1
who
MATLAB replies with the variables in your workspace
what
MATLAB replies with the current directory and MATLAB …les in the
directory
why
See yourself!!
who
MATLAB replies with the variables in your workspace
what
MATLAB replies with the current directory and MATLAB …les in the
directory
why
See yourself!!
help
The most important function for learning MATLAB on your own
Math-306 : Programming & Numerical Methods Lecture # 1
who
MATLAB replies with the variables in your workspace
what
MATLAB replies with the current directory and MATLAB …les in the
directory
why
See yourself!!
help
The most important function for learning MATLAB on your own
More on help later
Math-306 : Programming & Numerical Methods Lecture # 1
Variable Types
Variable Types
Variable Types
Variable Types
Variable Types
Variable Types
Variable Types
Variable Types
Naming variables
Naming variables
Naming variables
Naming variables
Naming variables
Naming variables
Built-in variables
Naming variables
Built-in variables
i and j can be used to indicate complex numbers
Naming variables
Built-in variables
i and j can be used to indicate complex numbers
pi has the value 3.1415926. . .
Naming variables
Built-in variables
i and j can be used to indicate complex numbers
pi has the value 3.1415926. . .
ans stores the last unassigned value (like on a calculator)
Naming variables
Built-in variables
i and j can be used to indicate complex numbers
pi has the value 3.1415926. . .
ans stores the last unassigned value (like on a calculator)
Inf and -Inf are positive and negative in…nity
Naming variables
Built-in variables
i and j can be used to indicate complex numbers
pi has the value 3.1415926. . .
ans stores the last unassigned value (like on a calculator)
Inf and -Inf are positive and negative in…nity
NaN represents ‘Not a Number’
Hello World
Hello World
Hello World
Hello World
Hello World
Introduction to MATLAB 9 / 43
Math-306 : Programming & Numerical Methods Lecture # 1
Hello World
Hello World
Scalars
Scalars
Scalars
Arrays
Arrays
Arrays
Arrays
Arrays
Matrices
Matrices
Matrices
Matrices
Matrices
Matrices
Matrices
save/clear/load
Use save to save variables to a …le
» save my…le a b
saves variables a and b to the …le my…le.mat, my…le.mat…le is in the
current directory
save/clear/load
Use save to save variables to a …le
» save my…le a b
saves variables a and b to the …le my…le.mat, my…le.mat…le is in the
current directory
Default working directory is
» nMATLABnwork
save/clear/load
Use save to save variables to a …le
» save my…le a b
saves variables a and b to the …le my…le.mat, my…le.mat…le is in the
current directory
Default working directory is
» nMATLABnwork
Create own folder and change working directory to it
» MyDocumentsn6.094nday1
save/clear/load
Use save to save variables to a …le
» save my…le a b
saves variables a and b to the …le my…le.mat, my…le.mat…le is in the
current directory
Default working directory is
» nMATLABnwork
Create own folder and change working directory to it
» MyDocumentsn6.094nday1
Use clear to remove variables from environment
» clear a b
Now look at workspace, the variablesa andb are gone
save/clear/load
Use save to save variables to a …le
» save my…le a b
saves variables a and b to the …le my…le.mat, my…le.mat…le is in the
current directory
Default working directory is
» nMATLABnwork
Create own folder and change working directory to it
» MyDocumentsn6.094nday1
Use clear to remove variables from environment
» clear a b
Now look at workspace, the variablesa andb are gone
Use load to load variable bindings into the environment
» load my…le
look at workspace, the variablesa andb are back
save/clear/load
Use save to save variables to a …le
» save my…le a b
saves variables a and b to the …le my…le.mat, my…le.mat…le is in the
current directory
Default working directory is
» nMATLABnwork
Create own folder and change working directory to it
» MyDocumentsn6.094nday1
Use clear to remove variables from environment
» clear a b
Now look at workspace, the variablesa andb are gone
Use load to load variable bindings into the environment
» load my…le
look at workspace, the variablesa andb are back
Can do the same for entire environment
» save myenv; clear all; load myenv;
Sqn Ldr Athar Kharal (H and S, CAE) Introduction to MATLAB 14 / 43
Math-306 : Programming & Numerical Methods Lecture # 1
Exercise: Variables
Exercise: Variables
Exercise: Variables
Exercise: Variables
Exercise: Variables
Exercise: Variables
Exercise: Variables
Arithmetic operations ( +, -, *, / )
» 7/45
» (1+i)*(2+i)
»1 / 0
»0 / 0
Arithmetic operations ( +, -, *, / )
» 7/45
» (1+i)*(2+i)
»1 / 0
»0 / 0
Exponentiation (^)
» 4^2
» (3+4*j)^2
Arithmetic operations ( +, -, *, / )
» 7/45
» (1+i)*(2+i)
»1 / 0
»0 / 0
Exponentiation (^)
» 4^2
» (3+4*j)^2
Complicated expressions, use parentheses
» ((2+3)*3)^0.1
Arithmetic operations ( +, -, *, / )
» 7/45
» (1+i)*(2+i)
»1 / 0
»0 / 0
Exponentiation (^)
» 4^2
» (3+4*j)^2
Complicated expressions, use parentheses
» ((2+3)*3)^0.1
Multiplication is NOT implicit given parentheses
» 3(1+0.7) gives an error
Arithmetic operations ( +, -, *, / )
» 7/45
» (1+i)*(2+i)
»1 / 0
»0 / 0
Exponentiation (^)
» 4^2
» (3+4*j)^2
Complicated expressions, use parentheses
» ((2+3)*3)^0.1
Multiplication is NOT implicit given parentheses
» 3(1+0.7) gives an error
To clear cluttered command window
» clc
Sqn Ldr Athar Kharal (H and S, CAE) Introduction to MATLAB 16 / 43
Math-306 : Programming & Numerical Methods Lecture # 1
Built-in Functions
Built-in Functions
Help/Docs
Help/Docs
Help/Docs
Help/Docs
Exercise: Scalars
You can tell the di¤erence between a row and a column vector by:
You can tell the di¤erence between a row and a column vector by:
Looking in the workspace
You can tell the di¤erence between a row and a column vector by:
Looking in the workspace
Displaying the variable in the command window
You can tell the di¤erence between a row and a column vector by:
Looking in the workspace
Displaying the variable in the command window
Using the size function
You can tell the di¤erence between a row and a column vector by:
Looking in the workspace
Displaying the variable in the command window
Using the size function
To get a vector’s length, use the length function
transpose
The transpose operators turns a column vector into a row vector and
vice versa
» a = [1 2 3 4]
» transpose(a)
transpose
The transpose operators turns a column vector into a row vector and
vice versa
» a = [1 2 3 4]
» transpose(a)
Can use dot-apostrophe as short-cut
» a.’
transpose
The transpose operators turns a column vector into a row vector and
vice versa
» a = [1 2 3 4]
» transpose(a)
Can use dot-apostrophe as short-cut
» a.’
The apostrophe gives the Hermitian-transpose, i.e. transposes and
conjugates all complex numbers
» a = [1+j 2+3*j]
» a’
» a.’
transpose
The transpose operators turns a column vector into a row vector and
vice versa
» a = [1 2 3 4]
» transpose(a)
Can use dot-apostrophe as short-cut
» a.’
The apostrophe gives the Hermitian-transpose, i.e. transposes and
conjugates all complex numbers
» a = [1+j 2+3*j]
» a’
» a.’
For vectors of real numbers .’and ’give same result
Element-Wise Functions
Element-Wise Functions
Element-Wise Functions
Element-Wise Functions
Element-Wise Functions
Element-Wise Functions
Operators: element-wise
Operators: element-wise
Operators: element-wise
2 3 2 3 2 3
1 4 4
4 2 5 . 4 2 5 = 4 4 5
3 1 3
3 1 . 3 1 = 3 1
2 3 2 3 2 3
1 1 1 1 2 3 1 2 3
4 2 2 2 5 . 4 1 2 3 5 = 4 2 4 6 5
3 3 3 1 2 3 3 6 9
3 3 . 3 3 = 3 3
Operators: standard
Multiplication can be done in a standard way or element-wise
Operators: standard
Multiplication can be done in a standard way or element-wise
Standard multiplication (*) is either a dot-product or an outer-product
Remember from linear algebra: inner dimensions must MATCH!!
Operators: standard
Multiplication can be done in a standard way or element-wise
Standard multiplication (*) is either a dot-product or an outer-product
Remember from linear algebra: inner dimensions must MATCH!!
Standard exponentiation (^) implicitly uses
Can only be done on square matrices or scalars
Operators: standard
Multiplication can be done in a standard way or element-wise
Standard multiplication (*) is either a dot-product or an outer-product
Remember from linear algebra: inner dimensions must MATCH!!
Standard exponentiation (^) implicitly uses
Can only be done on square matrices or scalars
Left and right division (/ n) is same as multiplying by inverse
Recommendation: just multiply by inverse (more on this later)
Operators: standard
Multiplication can be done in a standard way or element-wise
Standard multiplication (*) is either a dot-product or an outer-product
Remember from linear algebra: inner dimensions must MATCH!!
Standard exponentiation (^) implicitly uses
Can only be done on square matrices or scalars
Left and right division (/ n) is same as multiplying by inverse
Recommendation: just multiply by inverse (more on this later)
2 3
4
1 2 3 4 2 5 = 11
1
1 3 3 1=1 1
Operators: standard
Multiplication can be done in a standard way or element-wise
Standard multiplication (*) is either a dot-product or an outer-product
Remember from linear algebra: inner dimensions must MATCH!!
Standard exponentiation (^) implicitly uses
Can only be done on square matrices or scalars
Left and right division (/ n) is same as multiplying by inverse
Recommendation: just multiply by inverse (more on this later)
2 3
4
1 2 3 4 2 5 = 11
1
1 3 3 1=1 1
1 2 1 2 1 2
^2 =
3 4 3 4 3 4
Must be square to do powers
Sqn Ldr Athar Kharal (H and S, CAE) Introduction to MATLAB 26 / 43
Math-306 : Programming & Numerical Methods Lecture # 1
2 3 2 3 2 3
1 1 1 1 2 3 3 6 9
4 2 2 2 5 4 1 2 3 5 = 4 6 12 18 5
3 3 3 1 2 3 9 18 27
3 3 3 3 = 3 3
Automatic Initialization
Automatic Initialization
Automatic Initialization
Automatic Initialization
Automatic Initialization
Automatic Initialization
Automatic Initialization
Automatic Initialization
Automatic Initialization
Vector Indexing
Vector Indexing
Vector Indexing
Vector Indexing
Vector Indexing
Vector Indexing
Vector Indexing
Matrix Indexing
Matrix Indexing
b (1, 1) ! 14 33 b (1, 2)
b (2, 1) ! 9 8 b (2, 2)
Matrix Indexing
b (1, 1) ! 14 33 b (1, 2)
b (2, 1) ! 9 8 b (2, 2)
b (1) ! 14 33 b (3)
b (2) ! 9 8 b (4)
Matrix Indexing
b (1, 1) ! 14 33 b (1, 2)
b (2, 1) ! 9 8 b (2, 2)
b (1) ! 14 33 b (3)
b (2) ! 9 8 b (4)
Picking submatrices
Matrix Indexing
b (1, 1) ! 14 33 b (1, 2)
b (2, 1) ! 9 8 b (2, 2)
b (1) ! 14 33 b (3)
b (2) ! 9 8 b (4)
Picking submatrices
» A = rand(5) % shorthand for 5x5 matrix
Matrix Indexing
b (1, 1) ! 14 33 b (1, 2)
b (2, 1) ! 9 8 b (2, 2)
b (1) ! 14 33 b (3)
b (2) ! 9 8 b (4)
Picking submatrices
» A = rand(5) % shorthand for 5x5 matrix
» A(1:3,1:2) % specify contiguous submatrix
Matrix Indexing
b (1, 1) ! 14 33 b (1, 2)
b (2, 1) ! 9 8 b (2, 2)
b (1) ! 14 33 b (3)
b (2) ! 9 8 b (4)
Picking submatrices
» A = rand(5) % shorthand for 5x5 matrix
» A(1:3,1:2) % specify contiguous submatrix
» A([1 5 3], [1 4]) % specify rows and columns
Advanced Indexing 1
Advanced Indexing 1
Advanced Indexing 1
Advanced Indexing 1
Advanced Indexing 1
Advanced Indexing 1
Advanced Indexing 1
Advanced Indexing 1
There are functions to help you …nd desired values within a vector or
matrix e.g.
» vec= [1 5 3 9 7]
There are functions to help you …nd desired values within a vector or
matrix e.g.
» vec= [1 5 3 9 7]
To get the minimum value and its index:» [minVal,minInd] =
min(vec);
There are functions to help you …nd desired values within a vector or
matrix e.g.
» vec= [1 5 3 9 7]
To get the minimum value and its index:» [minVal,minInd] =
min(vec);
To get the maximum value and its index:» [maxVal,maxInd] =
max(vec);
There are functions to help you …nd desired values within a vector or
matrix e.g.
» vec= [1 5 3 9 7]
To get the minimum value and its index:» [minVal,minInd] =
min(vec);
To get the maximum value and its index:» [maxVal,maxInd] =
max(vec);
To …nd any of the indices of speci…c values or ranges:
» ind= …nd(vec== 9);
» ind= …nd(vec> 2 & vec< 6);
There are functions to help you …nd desired values within a vector or
matrix e.g.
» vec= [1 5 3 9 7]
To get the minimum value and its index:» [minVal,minInd] =
min(vec);
To get the maximum value and its index:» [maxVal,maxInd] =
max(vec);
To …nd any of the indices of speci…c values or ranges:
» ind= …nd(vec== 9);
» ind= …nd(vec> 2 & vec< 6);
To convert between subscripts and indices, useind2sub, andsub2ind.
Look up help to see how to use them.
Plotting Vectors
Example
» x=linspace(0,4*pi,10);
» y=sin(x);
Plotting Vectors
Example
» x=linspace(0,4*pi,10);
» y=sin(x);
Plot values against their index
» plot(y);
Plotting Vectors
Example
» x=linspace(0,4*pi,10);
» y=sin(x);
Plot values against their index
» plot(y);
Usually we want to plot y versus x
» plot(x,y);
Plotting Vectors
Example
» x=linspace(0,4*pi,10);
» y=sin(x);
Plot values against their index
» plot(y);
Usually we want to plot y versus x
» plot(x,y);
Plotting Vectors
Example
» x=linspace(0,4*pi,10);
» y=sin(x);
Plot values against their index
» plot(y);
Usually we want to plot y versus x
» plot(x,y);
-o .•
~.6
~8
·'0L_-~--'-'-L_~-~-~~L_-_j
Plot Options
Can change the line color, marker style, and line style by adding a
string argument
» plot(x,y,’g.-’);
Plot Options
Can change the line color, marker style, and line style by adding a
string argument
» plot(x,y,’g.-’);
Can plot without connecting the dots by omitting line style argument
» plot(x,y,’.’)
Plot Options
Can change the line color, marker style, and line style by adding a
string argument
» plot(x,y,’g.-’);
Can plot without connecting the dots by omitting line style argument
» plot(x,y,’.’)
Look at help document ofplot for a full list of colors, markers, and
linestyles
Exercise: Plotting
Exercise: Plotting
Exercise: Plotting
Thank you