0% found this document useful (0 votes)
10 views32 pages

Module02 MATLAB Training

The document outlines a MATLAB and Simulink training schedule for Module 3 held on March 13, 2020, at Diponegoro University. It includes an overview of topics such as vectors, matrices, logical programming, flow control, and symbolic tools, along with practical exercises and examples. Additionally, it covers the use of functions, if statements, for loops, switch statements, and while statements in MATLAB.

Uploaded by

Gokil Bro
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views32 pages

Module02 MATLAB Training

The document outlines a MATLAB and Simulink training schedule for Module 3 held on March 13, 2020, at Diponegoro University. It includes an overview of topics such as vectors, matrices, logical programming, flow control, and symbolic tools, along with practical exercises and examples. Additionally, it covers the use of functions, if statements, for loops, switch statements, and while statements in MATLAB.

Uploaded by

Gokil Bro
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MATLAB & SIMULINK TRAINING

Module 3
13 March 2020

Robotics and Automation Lab.


-1- Diponegoro University
Content

Date Time Activity Description Instructor


13.30 - 14.30 Module 2 Simulink Overview Joga

14.40 - 15.30 Module 3 Vector , Matrix Sabri


Friday 15.30 – 16.00 Logical Programming and Flow Control Sabri
13 March
16.00 - 16.30 Symbolic Tools Sabri
2020

Robotics and Automation Lab. Diponegoro University


-2-
REVIEW Module 1
Plotting Exercise
 Create an M file and execute the file.
 Know how to use commands:
 plot, grid, xlabel, ylabel, title, legend
 hold, figure, subplot
 close
 plot3
 How to write:
 Greek letter, subscript, superscript
 How to copy a figure to MS Word
 How to make other adjustments on a figure

Robotics and Automation Lab. Diponegoro University


-3-
REVIEW Module 1

Greek Letters, subscript and superscript


Execute an M file : coba_1plot.m
and type on the command window

>> ylabel(‘angle of attack, \alpha (deg)’)


>>xlabel(‘Time, \tau_1 (sec)’)

Also try to type on the command window the following

>> ylabel(‘angle of attack, \alpha^2 (deg)’)


>>xlabel(‘Time, \tau_{1a} (sec)’)

Observe the difference if you type

>> ylabel(‘angle of attack, \Alpha^2 (deg)’)


>>xlabel(‘Time, \Tau_1 (sec)’)

Robotics and Automation Lab. Diponegoro University


-4-
Module 2
Vector, Matrix, Logical Programming,
Flow Control & Symbolic Tool

Robotics and Automation Lab.


-5- Diponegoro University
Matrices and Vectors

 Didalam MATLAB, setiap data akan disimpan dalam bentuk matriks atau
vektor. Dalam membuat suatu data matriks pada matlab, setiap isi data
harus dimulai dari kurung siku buka “[“ dan diakhiri dengan kurung siku
tutup “]”. Dan untuk membuat variable dengan data yang terdiri
beberapa baris, gunakan tanda ‘titik koma’ (;) untuk memisahkan data
tiap barisnya.
Contoh pembuatan data matriks pada matlab:

>> A = [1 2 3] >> C = [1 2 3; 4 5
6] E=
A=

1 2 3
C= 1
2
4
5 ?
1 2 3 3 6
4 5 6
>> B = [1; 2;
3] >> D = [1 2; 3 4; 5 6
]
F=
B=

1
D=
1 3 5 ?
1 2 2 4 6
2
3 3 4
5 6
Robotics and Automation Lab. Diponegoro University
-6-
Matrices and Vectors

Change an element: Transpose: Diagonal matrices:

>> A = [1 2 3; 4 5 6; 7 8
>> A = [1 2 3; 3 4 5] >> A = [1 2 3; 4 5 6]
9]
A= A=
A=
1 2 3 1 2 3
1 2 3
3 4 5 4 5 6
4 5 6
7 8 9
>> A(2,1) = 6 >> B = A'
>> B = diag(A)
A= B=
B=
1 2 3 1 4
6 4 5 2 5
1
3 6
5
9

Robotics and Automation Lab. Diponegoro University


-7-
Matrices and Vectors
Create an empty array Calculate the determinant Augmented matrix
>> C = [-1 2 1; 3 4 5; -2 1 7]

C=
>> A = [1 2 3; 4 5 6; 7 8 9] >> B = [3 5 2; 6 4 5; 1 4 2]
-1 2 1
A= B= 3 4 5
-2 1 7
1 2 3 3 5 2
4 5 6 6 4 5 >> D = [12; 10; 11]
7 8 9 1 4 2
D=
>> A(2,:) = [] >> det(B)
12
A= ans = 10
11
1 2 3 -31
7 8 9 >> E = [C D]

E=

-1 2 1 12
3 4 5 10
-2 1 7 11

Robotics and Automation Lab. Diponegoro University


-8-
Range Operators

The range operators allows one to set up a vector of equally spaced entries

Defining vectors with the range operator Accessing data from arrays with the range operator

>> A = [1 2 3; 4 5 6; 7 8 9]
>> x = 1:4
A=
x=
1 2 3
1 2 3 4 4 5 6
7 8 9
>> y = 1:2:10
>> B = A(:,1)
y=
B=
1 3 5 7
9 1
4
7

Robotics and Automation Lab. Diponegoro University


-9-
Matrix indexing
>> A=[1 2 3; 4 5 6;7 8 9]
A=
1 2 3
4 5 6
7 8 9

Multiple row column indexing


The syntax
A([m],[n]) Linear indexing
Row column indexing The syntax
The syntax >> A([2,3],[2,3]) A(I)
ans =
A(m,n) 5 6 >> A(9)
8 9 ans =
>> A(1,3) 9
ans = Extract the entire 3rd column
3 >> A(:,end) >> A([7,8,9])'
ans = ans =
>> A(1,end) 3 3
ans = 6 6
3 9 9

Robotics and Automation Lab. Diponegoro University


- 10 -
Cell arrays
To concatenate elements into a cell array , use curly bracket { } instead of square bracket [ ]

Create cell array


>> x={pi,[];'foo',42;ones(2), eye(2)} Accessing cell Accessing contain
x=
>> y=x(2,2)
y= >> y=x{2,2}
[ 3.1416] [] y=
'foo' [ 42] [42] 42
[2x2 double] [2x2 double]

Robotics and Automation Lab. Diponegoro University


- 11 -
Size and Shape Operators

This operators access basic array information


Find the length of a vector Reshape a matrix

>> A =[1 2;3 4];


>> b = [1 2 3 4]; >> reshape(A,4,1)
>> length(b)
ans =
ans =
1
4 3
2
4
Find the size of a matrix
Find the number of dimensions of a matrix

>> A = [1 2; 3 4]; >> A = [1 2; 3 4];


>> size(A) >> ndims(A)

ans = ans =

2 2 2

Robotics and Automation Lab. Diponegoro University


- 12 -
Special Matrices

MATLAB menyediakan beberapa fungsi yang dapat kita gunakan untuk


menghasilkan bentuk-bentuk matriks yang diinginkan. Fungsi-fungsi tersebut
antara lain:
 zeros : untuk membuat matriks yang semua datanya bernilai 0

 ones : matriks yang semua datanya bernilai 1


 randn : matriks dengan data random dengan menggunakan distribusi
normal
 eye : untuk menghasilkan matriks identitas

>> C = rand(1,3)
>> A = zeros(1,3)
C=
A=
0.9501 0.2311 0.6068
0 0 0
>> D = eye(2,2)
>> B = ones(1,3)
D=
B=
1 0
1 1 1 0 1

Robotics and Automation Lab. Diponegoro University


- 13 -
How to Make Function

Bentuk penulisan nama fungsi : function [output] = nama_fungsi(input)

1 Ketik sintak dibawah ini dalam m-file:


function [a,b] = nama_fungsi(c,d,e)
a = c+d+e
b = c*d*e
end

2 Simpan sesuai dengan nama fungsi (nama_fungsi.m)

3 Ketik sintak dibawah ini pada command window


>> [a,b] = nama_fungsi(7,2,5)

a? b?
Robotics and Automation Lab. Diponegoro University
- 14 -
Practice

Buatlah fungsi luas lingkaran dan keliling lingkaran

function [A,C] = test_function(r)


A = pi*(r^2) % luas lingkaran
C = 2*pi*r % keliling lingkaran
end

>> [A,C] = test_function(6)

A=

113.0973

C=

37.6991

Robotics and Automation Lab. Diponegoro University


- 15 -
Practice

%Fungsi untuk menghitung segi-3 siku-siku: segitiga.m


%Untuk menghitung sisi miring, luas, dan keliling
function [Sisi_C,Luas,Kll] = segitiga(Sisi_A,Sisi_B)
% Menghitung sisi miring
Sisi_C = sqrt(Sisi_A^2 + Sisi_B^2);
% Menghitung luas segitiga
>> [Sisi_C,Luas,Kll] = segitiga(12,16)
Luas = 1/2* Sisi_A * Sisi_B;
% Menghitung keliling Sisi_C =
Kll = Sisi_A + Sisi_B + Sisi_C;
20

Luas =

96

Kll =

48

Robotics and Automation Lab. Diponegoro University


- 16 -
If Statements

MATLAB memiliki logical statement yang dapat digunakan untuk mengatur


aliran data pada fungsi yang akan dibuat :

if ekspresi1
statements1; a = -4
if a > 0
elseif ekspresi2 y = 2*a
statements2; elseif a == 0
y=a
else else a < 0
statements3; y = 2+a
end
end

Robotics and Automation Lab. Diponegoro University


- 17 -
If Statements (Practice)

Kategori nilai ujian mahasiwa: nilai = 75


Nilai ‘A’ untuk kisaran 91 ~ 100
Nilai ‘B’ untuk kisaran 81 ~90 if (nilai >= 91) & (nilai <= 100)
Nilai ‘C’ untuk kisaran 71 ~80
nilai = 'A'
Nilai ‘D’ untuk kisaran 61 ~70
dan elseif (nilai >= 81) & (nilai <= 90)
Nilai ‘E’ jika nilai mahasiswa kurang dari 60 nilai = 'B'
elseif (nilai >= 71) & (nilai <= 80)
nilai = 'C'
elseif (nilai >= 61) & (nilai <= 70)
nilai = 'D'
else nilai <= 60
nilai = 'E'
end

Robotics and Automation Lab. Diponegoro University


- 18 -
For Loop

0.9
x = -1:0.01:1;
0.8
for i=1:length(x)
if x(i) < 0.5 0.7

F(i) = x(i)^2; 0.6

else 0.5
F(i) = 0.25; 0.4
end
0.3
end
plot(x,F) 0.2

0.1

0
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1

Robotics and Automation Lab. Diponegoro University


- 19 -
Switch Statements

switch expression method = ‘linear';


case 1
do these statements switch (method)
case 2 case {'linear','bilinear'}
do these statements disp('Method is linear')
case n case 'cubic'
do these statements disp('Method is cubic')
end case 'nearest'
disp('Method is nearest')
otherwise
disp('Unknown method.')
end

Robotics and Automation Lab. Diponegoro University


- 20 -
While Statements

figure;
x=linspace(0,4,500);
A=0.5:0.5:2; 4
i=2;
while i <= length(A) 3.5

y = A(i)* x.^(1/2); 3
plot(x,y); hold on;
i=i+1; 2.5

end 2
grid on;
1.5

0.5

0
0 0.5 1 1.5 2 2.5 3 3.5 4

Robotics and Automation Lab. Diponegoro University


- 21 -
Module 3
Symbolic Tool

Robotics and Automation Lab.


- 22 - Diponegoro University
Matrix symbolic

>> det(A)
>> syms a b c d
>> A=[a b; c d] ans =

a*d - b*c
A=
>> inv(A)

[ a, b] ans =
[ c, d]
[ d/(a*d - b*c), -b/(a*d - b*c)]
[ -c/(a*d - b*c), a/(a*d - b*c)]

Robotics and Automation Lab. Diponegoro University


- 23 -
Solving Algebraic Equations
y = 3x
ax + 5 = 0 2 x + 3 x + 5 x = 160 y = 2x +5
>> solve('a*x + 5 = 0') >> solve('2^x+3^x+5^x=160') >> eq1=('y=3^x')
ans = eq1 =
ans = 3.0000 y=3^x

>> eq2=('y=2^x+5')
-5/a log( x − 2) eq2 =
=4
log 2 y=2^x+5

x 2 − 6 x − 12 = 0 log( x − 2) = 4 log 2 >> s=solve(eq1,eq2)


s=
x: [1x1 sym]
>> solve('x^2 - 6*x - 12 = 0') >> solve('log10(x-2)= y: [1x1 sym]

4*log10(2)') >> x=s.x


ans = ans = x=
18 2.0000
3+21^(1/2) >> y=s.y
3-21^(1/2) y=
9.0000

Robotics and Automation Lab. Diponegoro University


- 24 -
Equation Solving
x+ y =5 ( x / a ) 2 + ( y / b) 2 = 1
y − x =1 5 x + 2b = c
>> syms x y >> syms x y a b c
>> f=[x+y-5;y-x-1] >> sol.x >> f=[(x/a)^2+(y/b)^2-1; 5*x+2*b-c]
f=
x^2/a^2 + y^2/b^2 - 1
f= ans = 2*b - c + 5*x

>> sol=solve(f,x,y)
x+y-5 2 sol =
y-x-1
x: [2x1 sym]
>> sol.y y: [2x1 sym]
>> sol=solve(f,x,y)
>> [sol.x,sol.y]
sol = ans = ans =
[ c/5 - (2*b)/5, (b*((4*(5*a - 2*b + c)*(
5*a + 2*b - c))/25)^(1/2))/(2*a)]
x: [1x1 sym] 3 [ c/5 - (2*b)/5, -(b*((4*(5*a - 2*b + c)*(
y: [1x1 sym] 5*a + 2*b - c))/25)^(1/2))/(2*a)]

Robotics and Automation Lab. Diponegoro University


- 25 -
Equation Solving

>> syms x y z >> [x,y,z] = solve(eq1,eq2,eq


>> eq1 = '2*x-3*y+4*z = 5' 3,x,y,z)

eq1 = x=

2*x-3*y+4*z = 5 -5/37

>> eq2 = 'y+4*z+x = 10'


y=
eq2 =
45/37
y+4*z+x = 10

>> eq3 = '-2*z+3*x+4*y = 0' z=

eq3 = 165/74

-2*z+3*x+4*y = 0

Robotics and Automation Lab. Diponegoro University


- 26 -
Plotting Symbolic Equations

x 2 + 5x + 6 = 0 x >> syms x
y= >> y=x/(x+5
>> y = 'x^2 + 5*x + 6'; x+5 );
>> ezplot(y) >> ezplot(y)
x2 + 5 x + 6
>> grid
x/(x+5)
80
3

70
2

60
1

50
0

40
-1
30
-2
20
-3
10
-4
0

-6 -4 -2 0 2 4 6
-6 -4 -2 0 2 4 6
x
x

Robotics and Automation Lab. Diponegoro University


- 27 -
Limit & differential
x3 +1
lim
x →0 x + 2
4

>> syms x syms x t


>> limit((x^3 + 1)/(x^4 + 2)) f = x^3+5*x^
2;
ans = g = sin(10*t);
1/2 diff(f)
diff(g)
lim (x + 5)
x →3 ans =
3*x^2 + 10*x
>> syms x
>> limit(x+5,3) ans =
ans = 10*cos(10*t)

Robotics and Automation Lab. Diponegoro University


- 28 -
Differential

ans =
syms x t
f = x^2; 2*x
g = sin(10*t);
diff(f) ans =
diff(g)
10*cos(10*t)

y = x + 2x + 6x + 5
3 2 y = 2(sin t ) 2

y' = ? y' = ?

Robotics and Automation Lab. Diponegoro University


- 29 -
Indefinite & definite integral

 dx
2
x
6

 cos xdx
2
>> int('x^2') x
−6
ans =
>> syms x
1/3*x^3 >> f = x^2*cos(x)

 asin (t)dt f=
x^2*cos(x)

>> a = int(f,-6,6)
>> int('a*sin(w*t)') a=
68*sin(6)+24*cos(6)
ans =
>> double(a)
-a/t*cos(w*t) ans =
4.0438
>> pretty(ans)

Robotics and Automation Lab. Diponegoro University


- 30 -
Laplace Transforms
>> syms t
f ( x) = t 2
>> f = t^2
f ( x) = t
>> syms t f=
>> laplace(t)
t^2
ans =

1/s^2 >> laplace(f)

ans =

2/s^3
>> ilaplace(ans
)
>> ilaplace(ans)
ans =
ans =
t
t^2

Robotics and Automation Lab. Diponegoro University


- 31 -
Equation solving

Jacobian matrics  f1 f1 


 x y 
 f1   x 2 + sin( xy ) + 1  
f = = 2   f 2 f 2 
 f2   y + e + 4 
xy
 x y 
>> syms x y
>> f=[x^2+sin(x*y)+1,y^2+exp(x*y)+4]
f=
sin(x*y) + x^2 + 1, exp(x*y) + y^2 + 4]

>> jacobian(f)
ans =
[ 2*x + y*cos(x*y), x*cos(x*y)]
[ y*exp(x*y), 2*y + x*exp(x*y)]

Robotics and Automation Lab. Diponegoro University


- 32 -

You might also like