Chapter 1.
How to use MATLAB
Assistant. Min yeong Choi
Division of Electronics and Electrical Engineering
Dongguk University – Seoul Campus
1st semester, 2024
1
Outline
Time
• Mon. 10:30 – 12:00
Classroom
• 원흥관 318 ( 공용컴퓨터실 )
Course Assistant
• Min yeong Choi (010.4066.2453, himy01@dgu.ac.kr, Room No. 8134 )
2
Outline
References:
• “ 제대로 배우는 MATLAB,” ( 방성완 , 한빛아카데미 , 2014)
• “The MathWorks, Inc - MATLAB Control System Toolbox Reference
(2018, The MathWorks, Inc)”
3
How to use MATLAB
1. Make a new “MathWorks” account
http://kr.mathworks.com
4
How to use MATLAB
1. Make a new “MathWorks” account
5
How to use MATLAB
2. Activate the “MathWorks” account
6
How to use MATLAB
2. Activate the “MathWorks” account
7
How to use MATLAB
3. Type “ID” and Password”
Type student’s personal
dongguk university e-mail
address
(********@dongguk.edu)
Type pass-
word
8
How to use MATLAB
4. Download the MATLAB program
Click
9
How to use MATLAB
4. Download the MATLAB program
10
How to use MATLAB
4. Download the MATLAB program
11
How to use MATLAB
5. Type the University e-mail address
Type student’s personal dongguk
university e-mail address
(********@dongguk.edu)
12
How to use MATLAB
6. Select the License type (Individual)
13
How to use MATLAB
7. Select the program (MATLAB & Simulink)
Check List
MATLAB
Simulink
Scroll down
Simscape
and check
Simulink Electrical
Simulink Control Design
Statistics and Machine
Leaning Tools
Symbolic Math Toolbox
Control System Toolbox
14
How to use MATLAB
MATLAB:
• This is a semi-word of the MATrix LABoratory, a computing tool that receives the matrix as ba-
sic input data and performs the process.
• Numerical analysis program optimized for matrix processing compared to C or FORTRAN.
(Commands [MATLAB] ↔ Functions in C, Subroutines FORTRAN)
• Recently, Various tool-boxes (SIMULINK, Control SystemToolbox, Optimization Toolbox, ETC.) have been widely used
to solve various engineering problems.
15
How to use MATLAB
Current Folder Workspace
Command Window
(CW)
Window role
Current Folder Matlab displays the folder on which m-files are refer-
enced.
Command Window A window that receives user input.
Workspace To display information about variables.
16
How to use MATLAB
How to set up a path
• [ Home - Set Path(Environment) ]
• [Add Folder] → [Save]
• It is important to set a path to
the working directory.
Because the path must be
configured to run the file.
• If the path is set incorrectly,
CW can’t run the file.
17
How to use MATLAB
Editor(filename.m
)
Upper side Left side Undocking
• Editor can move wherever you want to place by using drag and drop.
18
How to use MATLAB
Editor(filename.m
)• Making a editior
• 1. HOME • 2. New Editor
19
How to use MATLAB
Command Window(CW) vs Editor(M-file)
• CW • Editor
20
How to use MATLAB
Command Window(CW) vs Editor(M-file)
• Caution
Suppose that you are using the editor.
Then, if your m-file is in the directory but it is not running,
Please check that you have properly defined your Set Path.
21
How to use MATLAB
who, whos
• It is difficult to remember all the variables and their values currently
in use.
• One of the best features of MATLAB is that all defined variables are
always stored in a workspace.
• who : you can see the variables stored in the workspace.
• whos : to see the of the variables in more detail.
22
How to use MATLAB
who, whos
• Size=length(row)length(column)
• Class: Type of variables (Float,
Double, integer, etc.)
Whos Who
23
How to use MATLAB
Clear, Save, Load
• Clear: It can remove variables that you do not need to have mem-
ory capacity (clear variablename)
• Save: It can store variables in a file format.
(save filename variablename)
• Load: It can be used if you want to reuse data created by other
programs or stored in previous tasks. (load “saved filename”)
24
How to use MATLAB
Clear, Save, Load
• save bowling: Workspace can be saved as
the file “bowling.mat” in your current file
path.
• clear: All variables have been deleted.
• who: It can view a list of variables.
• load bowling: It can recall variables from
files that were previously saved.
25
How to use MATLAB
Format short, Format long, Format short e
Type Digits
Short 4
Short e 4 (Scientific Notation)
Long 15
{ P∙ e + x , : e + ( x )=10 x }
26
How to use MATLAB
Help, Lookfor
• Help: If you know the command but don't know how to operate it, you
can use the help command.
• Lookfor: This command can be used when you need general help rather
than help command.
Exam-
ple)
Help Instructor Lookfor instructor
27
How to use MATLAB
General symbol
Type Digits
>> Prompt
A Transposed matrix of A
‘text’ Text string
[1, 3; 2, 4] Matrix
[1 , 3] Row vector
[1 ; 3] Column vector
A(1,2) Row 1, Column 2 – element of A
%A is the matrix Comments
A is the matrix; End of the line
t = 0:0.1:1 Start value: 0,
Final value: 1,
Interval between elements: 0.1
A.*B Element-wise multiplication of A, B
28
How to use MATLAB
Relational Operator & Logical Operator
(Relational)
• A < B : A is smaller than
• A <= B: A is smaller than or equal to B
• A > B: A is larger than B
• A >= B: A is larger than or equal to B
• A == B: A is equal to B
• A ~= B: A is not equal to B
(Logical)
• A & B: A and B
• A | B: A or B
• ~A: not A
29
How to use MATLAB
Vectors
• Variablename = [ ]
Example)
1. a = ,
2. b =
= [1.4142, 0.2857, 2.0000] ( a(1,2) is the 1st row - 2nd column element of ‘a’. )
• Also, you can make a matrix which is mixed with row or column vec-
tors.
Example) c = [b; a] =
30
How to use MATLAB
Vectors
• ':' is a symbol that extracts some elements of a vector.
Example)
a=
b=
c = a(2:3)
d = b(1:2)
e = [a(2:3) ; b(1:2)]
f = [c ; d]
31
How to use MATLAB
Vectors
Q) Then, How do you make a vector whose elements are listed in a
given rule?
A) Vectorname = [Initial value : Growth rate : Final value]
𝛑 𝛑 𝛑 𝛑
𝟐 𝟐 𝟐 𝟐
32
How to use MATLAB
Vectors
• The other types of vector
1. linspace(Initial value, Final value, The number of elements)
Example) [0:0.1:1] = linspace(0,1,11)
[0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1]
2. logspace(Initial value, Final value, The number of elements)
Example) logspace(-1,1,5) = linspace(0.1,10,5)
33
How to use MATLAB
Vectors
• Calculation of vectors
34
How to use MATLAB
Vectors
• Example) How to draw a cosine function
35
How to use MATLAB
Vectors
• Example)
36
How to use MATLAB
Matrices
• How to make a matrix
‘ ; ’ means to go to the next row.
37
How to use MATLAB
Matrices
• How to extract a matrix composed of elements in a matrix
38
How to use MATLAB
Matrices
• Calculation of some matrices
1. Addition 2. Subtraction 3. Transposed
39
How to use MATLAB
Matrices
• Calculation of some matrices
4. Power of matrix 5. Multiplication
40
How to use MATLAB
Matrices
• Calculation of some matrices
6. Invertible matrix
41
How to use MATLAB
Matrices
Example) Matrix Computation
42
How to use MATLAB
Matrices
• Elementwise multiplication
Example) Calculate when
This computation method is called “Element-
wise multiplication”
43
How to use MATLAB
Matrices
• Special functions on matrices
Name Function Name Function
It can make a It can insert
ones(m,n) m n matrix with diag(v) a vector v into
all 1’s. diagonal elements.
It can make a
It can make a inverse matrix
zeros(m,n) m n matrix with inv(A)
of A.
all 0’s.
It can calculate
It can make a nn
eye(n) eig(A) an eigenvalue and an eigen-
identity matrix.
vector of a matrix A
44
How to use MATLAB
Matrices
Example)
45
How to use MATLAB
Matrices
Exercise) Use commands ‘zeros, eye, diag' to make a matrix H.
[ ]
0 .1 1 1
0 0 .1 1
0 0 0.1
46
How to use MATLAB
☞ Exercise)
1) If the values of the matrix A and B are given as shown in
'mat 1_1.m’, Write and run a program to calculate the fol-
lowing formula.
2) Save, name it ‘temp.m’ and run on CW.
47
How to use MATLAB
☞ Exercise)
1)
2) Just save, name it ‘temp.m’ and run on CW.
48
How to use MATLAB
For, While
• For, While loop is used to perform the calculations of a formula sen-
tence over and over again.
• Structure
for ( Variable = Start :
Final ) ,
statement ;
end
while( Condition ) ,
statement ;
end
49
How to use MATLAB
For loop
Example) Let’s make a 3 by 2 matrix.
Row loop
Column loop
* real(A): Real value of , imag(A): Imaginary value of when
50
How to use MATLAB
While loop
Example) Let’s calculate a sine function y =from 0 to .
t(k) : row vector (x-
axis)
y(k) = row vector
(y-axis)
51
How to use MATLAB
While loop
Exercise) Plot the cosine function y = ) A
Because of t(20) = 20!<20, So
‘While loop’ have been ended,
and length(y) is not 20 but 19.
plot(t,y,…. ) →
plot(y, ….)
52
How to use MATLAB
If loop
Exercise) Plot the unit step function at t = 2.
If ( Condition ) ,
Statement 1
else if (Condition)
Statement 2
else
Statement 3
end
53