Introduction To MATLAB
Introduction To MATLAB
Introduction To MATLAB
1
Outline
2
Editor Workspace
Current Directory
Command Window
Details
3
MATLAB R2018a. Courtesy of The MathWorks, Inc. Used with permission. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective
Customization
●
4
MATLAB R2018a. Courtesy of The MathWorks, Inc. Used with permission. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective
Installing Toolboxes
●
● ○
○
○
○
○
○
5
MATLAB R2018a. Courtesy of The MathWorks, Inc. Used with permission. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective
Making Folders
●
●
MATLAB
↳ Signals and Systems Lab
↳ Lab1_Intro2MATLAB 6
MATLAB R2018a. Courtesy of The MathWorks, Inc. Used with permission. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders.
Help/Docs
● help
○
●
○ help sin
■
●
○ doc sin
●
○ docsearch sin trigonometric
7
Outline
8
Scripts: Overview
●
○
○
○
●
○edit MyFileName.m
○
9
Scripts: Some notes
●
○
○
○
○
●
●
10
Exercise: Scripts
●
●
Hello world!
I am going to learn MATLAB!
disp(...)
'This is a string'
11
Outline
12
Variable Types
●
○
●
○
■
○
■
●
●
13
Naming Variables
●
myNumberVariable = 3.14
myStringVariable = 'hello world!'
●
○
○
○ var1 Var1
14
Naming Variables (cont.)
i, j:
pi:
ans:
Inf, -Inf:
NaN:
16
Arrays
●
●
○
○
17
Row vectors
●
○ row = [ 1 2 3.2 4 6 5.4 ];
○ row = [ 1, 2, 4, 7, 4.3, 1.1 ];
●
18
MATLAB version 6.5. Courtesy of The MathWorks, Inc. Used with permission. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders.
Column vectors
●
○ col = [ 1; 2; 3.2; 4; 6; 5.4 ];
19
MATLAB version 6.5. Courtesy of The MathWorks, Inc. Used with permission. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be trademarks or registered trademarks of their respective holders.
Size and length
●
○
○
○
20
Matrices
●
○
■ a= [1 2;3 4];
●
a = [1 2];
b = [3 4];
c = [5;6];
d = [a;b];
e = [d c];
f = [[e e];[a b a]];
str = ['Hello, I am ' 'John'];
● 21
save/clear/load
●
○save myFile a b
○
○
○
Signals and Systems
● Lab\Lab1_Intro2MATLAB
○clear a b
○
●
○load myFile
○
22
Outline
23
Basic Scalar Operations
●
○ 7/45
○ (1+1i)*(1+2i)
○ 1/0
○ 0/0
●
○ 4^2
○ (3+4*1j)^2
●
○ ((2+3)*3)^0.1
24
Built-in Functions
●
●
○sqrt(2)
○ log(2), log10(0.23)
○ cos(1.2), atan(-.8)
○ exp(2+4*1i)
○ round(1.4), floor(3.3), ceil(4.23)
○angle(1i); abs(1+1i);
25
Transpose
●
○ a = [1 2 3 4+i]
○transpose(a)
○a'
○ a.'
●
○
●
○
26
Addition and Subtraction
●
●
28
Addition and Subtraction
● c = row + column
● c = row.’ + column
● c = row + column.’
● s=sum(row);
● p=prod(row);
29
Element-wise functions
●
○
●
○
○
29
Element-wise functions
●
●
a=[1 2 3];b=[4;2;1];
30
Operators
●
●
○
●
●
○
31
Automatic Initialization
• Initialize a vector of ones, zeros, or random numbers
» o=ones(1,10)
➢ Row vector with 10 elements, all 1
» z=zeros(23,1)
➢ Column vector with 23 elements, all 0
» r=rand(1,45)
➢ Row vector with 45 elements (uniform (0,1))
» n=nan (1,69)
➢ Row vector of NaNs (representing uninitialized variables)
32
Automatic Initialization
• To initialize a linear vector of values use linspace
» a=linspace(0,10,5)
➢ Starts at 0, ends at 10 (inclusive), 5 values
» x=[12 13 5 8];
Matrix Indexing
• Matrices can be indexed in two ways
➢ using subscripts (row and column)
➢ using linear indices (as if matrix is a vector)
• Matrix indexing: subscripts or linear indices
b(1,1) b(1,2)
b(1,1) b(1,2) b(1)
b(1 b(3
b(3)
b(2,1)
b(2,2) b(2 ) b(4 )
• Picking submatrices
35
36
Advanced Indexing 2
• MATLAB contains functions to help you find desired values
» vec = [5 3 1 9 7]
• To get the minimum value and its index (similar for max):
» [minVal,minInd] = min(vec);
38
Plotting
• Example
» x=linspace(0,4*pi,10);
» y=sin(x);
40
End of Lecture 1
(1) Getting Started
(2) Scripts
(3) Making Variables
(4) Manipulating Variables
(5)BHope that wasn’t too much and
you enjoyed it!!
42