Introduction To MATLAB

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 41

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:

ii, jj, kk,


15
Scalars

○ a = 10


○ c = 1.3 * 45 - 2 * a

○cooldude = 13/3;

16
Arrays



MATLAB makes vectors easy!


That’s its power!

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];

a.*b , a./b , a.^b → all errors

a.*b.', a./b.’, a.^(b.’) → all valid

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

• Can also use colon operator (:)


» b=0:2:10
➢ Starts at 0, increments by 2, and ends at or before 10
➢ Increment can be decimal or negative
» c=1:5
➢ If increment is not specified, default is 1

• To initialize logarithmically spaced values use logspace


41 ➢ Similar to linspace, but see help
Vector Indexing
• MATLAB indexing starts with 1, not 0
➢ We will not respond to any emails
where this is the problem.
• a(n) returns the nth element

a(1) a(2) a(3) a(4)

• The index argument can be a vector. In this case,


each element is looked up individually, and returned
as a vector of the same size as the index vector.
34

» 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

» A = rand(5) % shorthand for 5x5 matrix


Advanced Indexing 1

• To select rows or columns of a matrix, use the :

» d=c(1,:); d=[12 5];


» e=c(:,2); e=[5;13];
» c(2,:)=[3 6]; %replaces second row of c

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);

• To find the indices of specific values or ranges


» ind = find(vec == 9); vec(ind) = 8;
» ind = find(vec > 2 & vec < 6);
➢ find expressions can be very complex, more on this later
➢ When possible, logical indexing is faster than find!
➢ E.g., vec(vec == 9) = 8; 37
Outline
(1) Getting Started
(2) Scripts
(3) Making Variables
(4) Manipulating Variables
(5) Basic Plotting

38
Plotting
• 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);

MATLAB makes visualizing data


fun and easy!
39
What does plot do?
• plot generates dots at each (x,y) pair and then connects the dots
with a line
• To make plot of a function look smoother, evaluate at more points
» x=linspace(0,4*pi,1000);
» plot(x,sin(x));
• x and y vectors must be same size or else you’ll get an error

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

You might also like