MATLAB untuk GURU SMA/SMK
Hari Sutiksno SURABAYA, 10 OKTOBER 2018
SEKOLAH TINGGI TEKNIK SURABAYA
1
Singkatan dari MATrix LABoratory • • • • •
Scientific programming environment Very good tool for the manipulation of matrices Great visualisation capabilities Loads of built-in functions Easy to learn and simple to use 2
STARTING MATLAB
3
4
MATLAB
5
OPERATOR
6
MARI KITA MULAI % This is a comment >> ((1+2)*3 - 2^2 - 1)/2 ans: 2 % Use ; to suppress output (scripts and functions) >> ((1+2)*3 - 2^2 - 1)/2; No output
% You need to use the ... operator to wrap lines >> 1 + 2 + 3 + 4 + 5 ... +6+7+8+9 ans: 45 7
Logic and Assignment % Assignment with equality >> a = 5; No Output % Logical test like >, <, >=, <=, ~= >> a == 6 ans: 0 % 0 is false in Matlab >> a ~= 6 ans: 1 % 1 is true in Matlab not( a == 6 ) also works 8
Logical Operators % Short Circuited Logic >> true || (slow_function) ans: 1 % Evaluates Quickly >> true | (slow_function) ans: 1 % Evaluate slowly % Matrix logic >> matrix1 || matrix2 ans: Error >> matrix1 | matrix2 Pair wise logic 9
Round, Floor, Ceil, Fix Round
10
Round
11
Fix
12
Ceil
13
Mod and Rem
14
rem >> rem(15,3) ans = 0 >> rem(15,4) ans = 3
15
POLY, ROOTS, CONV >> poly([1 2 3]) ans = 1 -6 11 -6 >> roots([1 3 2]) ans = -2 -1
>> conv([1 3 2],[1 4]) ans = 1 7 14 8 16
Residue
17
Complex Number >> a = 3i+6 a = 6.0000 + 3.0000i >> b = 5-2i b = 5.0000 - 2.0000i >> (a+2i)^3 ans = -2.3400e+002 +4.1500e+002i >> exp(pi*i/2) ans = 0.0000 + 1.0000i
18
19
MATRIX % A simple array >> [1 2 3 4 5] ans: 1 2 3 4 5 >> [1,2,3,4,5] ans: 1 2 3 4 5 >> 1:5 ans: 1 2 3 4 5 >> 1:2:5 ans: 1 3 5 >> 5:-2:1 ans: 5 3 1
20
Accessing Matrices Elements % Make a matrix >> A = [1 2 3; 4 5 6; 7 8 9] >> A(1,:) ans: 1 2 3 % Access Individual Elements >> A([1, 3, 5]) ans = 1 7 5 >> A( [1,3], 2:end ) ans = 2 3 8 9 % Semuanya adalah data yang sama >> [1 2 3; 4 5 6; 7 8 9] >> [1,2,3; 4,5,6; 7,8,9] >> [[1 2; 4 5; 7 8] [3; 6; 9]] >> [[1 2 3; 4 21
% Creating all ones, zeros, or identity matrices >> zeros( rows, cols ) >> ones( rows, cols ) >> eye( rows )
% Creating Random matrices >> rand( rows, cols ) >> rand(1,2) ans = 0.1419 0.9157 0.4218 0.7922 >> randi(10,4) ans = 6 7 8 5 3 2 1 5 8 4 10 5 2 7 8 4 22
% Make 3x5 with N(1, 4) entries >> 1 + 2 * randn(3,5) Ans: 2.0753 2.7243 0.1328 6.5389 2.4508 4.6678 1.6375 1.6852 -1.6998 0.8739 -3.5177 -1.6154 8.1568 7.0698 2.4295 randi(10,[4 3]) ans = 7 7 10 9 1 1 4 7 5 8 4 5 % Get the size >> Matrix=[4 3 5;2 1 8]; >> [baris, kolom]=size(Matrix) ans= ? 23
% Make a matrix >> A = [1 2 3; 4 5 6; 7 8 9] A= 1 4 7
2 5 8
3 6 9
4 5 6
7 8 9
>> A' ans = 1 2 3
>> A(:)’ ans: 1 4 7 2 5 8 3 6 9
24
>> A=[1 2 3;4 6 7; 1 4 -8]
ans =
A= 1 4 1
>> inv(A)
2 6 4
3 7 -8
Columns 1 through 2
>> A'
-2.3750 0.8750 1.2188 -0.3438 0.3125 -0.0625
ans =
Column 3
1 2 3
4 6 7
1 4 -8
-0.1250 0.1563 -0.0625
>> det(A)
>> diag(A)
ans =
ans =
32
1 6 -8
25
Carilah x , y dan z dari persamaan berikut:
x + 2 y - 5z = 1 3x - y + 4 z = 3 5 x + 3 y + 2 z = 23 >> A=[1 2 -5;3 -1 4;5 3 23]
>> inv(A)*B
A=
ans =
1 3 5
2 -1 3
-5 4 2
3.0000 2.0000 1.0000
>> B=[2;11;23] B= 2 11 23
26
Menggambar GRAFIK
27
PLOT sebuah grafik t = 0:pi/100:2*pi; y = sin(t);plot(t,y)
28
PLOT 3 buah grafik >> t = 0:pi/100:2*pi; >> y2 = sin(t-0.25);y3 = sin(t-0.5);plot(t,y,t,y2,t,y3)
29
PLOT 3 grafik dengan tanda berbeda >> t = 0:pi/100:2*pi; >> y2 = sin(t-0.25);y3 = sin(t-0.5);plot(t,y,t,y2,t,y3) >> plot(t,y,'',t,y2,'--',t,y3,':')
30
SKALA SEMILOG VERTIKAL >> x=1:100; >> semilogy(x)
31
SKALA SEMILOG HORIZONTAL >> x=1:100; >> semilogx(x)
32
SKALA LOG-LOG >> x=1:100; >> loglog(x,x.^2)
33
>> Y = [5 2 18 7 39 8 65 5 54 3 2]; >> bar(Y)
34
>> Y = [5 2 18; 7 39 8; 65 5 54; 100 3 2]; >> bar3(Y)
35
>> t=0:10:100; >> x=t.^2; >> stem(t,x,'fill')
36
Contoh: >> t = 1:60; >> x = linspace(0,2*pi,60); >> a = sin(x);b = cos(x) >> stem(t,a+b); >> hold on >> plot(t,[a' b']) >> legend('a + b','a = sin(x)','b =cos(x)') >> xlabel('Time in \musecs') >> ylabel('Magnitude'); >> title('Linear Combination of Two Functions') 37
Hasil
38
MATLAB SYMBOLIC MATH TOOLBOX
39
MATLAB Symbolic Math Toolbox q q q q q q q q
Symbolic Objects Kalkulus (Diferensial, limit, integral dan deret Taylor) Penyederhanaan fungsi Penyelesaian Persamaan Linier/Nonlinier Penyelesaian Persamaan Diferensial Variable-precision arithmetic Transformasi Fourier, Laplace, dan z Grafik
40
1. SYMBOLIC OBJECTS Symbolic objects present symbolic variables, symbolic numbers, symbolic expressions and symbolic matrices.
• Symbolic Variables To declare variables x and y as symbolic objects use the syms command: Contoh 1:
41
• Symbolic Numbers Contoh 2:
• Symbolic expressions Contoh 3:
42
• Symbolic Matrices Contoh 4:
43
• Symvar
44
Contoh 5:
45
2. Calculus 2.1. Diferensial
46
Contoh 5:
47
Contoh 6:
48
Contoh 7:
49
Contoh 8:
50
Rangkuman Diferensial
51
2.2. Limit
52
Contoh 9:
Jawab 9a:
53
Jawab 9b:
Jawab 9c:
54
Jawab 9d:
Jawab 9e:
55
Rangkuman Limit
56
2.3. Integral
57
Contoh 10. Indefinite Integral
58
Contoh 11. Indefinite Integral
59
Contoh 12. Definite Integral
60
Contoh 13. Definite Integral
61
Rangkuman Integral
62
2.4. Deret Taylor
63
Contoh 14:
64
Contoh 15:
65
Rangkuman Deret Taylor
66
3. Simplication
67
3.1. Coeffs
68
Contoh 16 :
69
3.2. Collect
70
Contoh 17 :
71
3.3. Expand
72
Contoh 18 :
Contoh 19 :
73
Contoh 20 :
74
3.4. Factor
75
Contoh 21:
Contoh 22:
76
Contoh 23:
77
3.5. Simplify
78
Contoh 24:
Contoh 25:
79
Contoh 26:
Contoh 27:
80
Contoh 28:
81
3.6. Simple
82
Contoh 29:
83
Contoh 30:
84
Contoh 31:
Contoh 32:
85
3.7. Subexpr
86
Contoh 33:
87
3.8. Subs
88
Contoh 34:
Contoh 35:
89
Contoh 36:
90
4. Solution of Equations 4.1. Compose
91
Contoh 37:
92
Contoh 38 :
93
Contoh 39 :
94
4.2. Solve
95
Contoh 40:
96
Contoh 41:
97
Contoh 42:
98
Contoh 43:
x=2 y=5 99
4.3 Dsolve
100
Contoh 44:
y = Ae
-2 x
+ Be
-4 x
101
Contoh 45:
Contoh 46:
102
Contoh 47:
103
4.4 Finverse
104
Contoh 48:
105
5. Variable Precision Arithmetic
106
Contoh 49:
107
Contoh 50:
108
6. TRANSFORMASI INTEGRAL 6.1. Transormasi Fourier
109
Contoh 51:
Contoh 52:
110
Contoh 53:
111
6.2 Inverse Fourier Transform
112
Contoh 54:
113
6.3. Transformasi Laplace
114
Contoh 55:
115
6.4. Inverse Transformasi Laplace
116
Contoh 56:
117
6.5. Transformasi z
118
Contoh 57:
119
Contoh 58:
1 z Z [( ) k ] = 1 2 z2 120
6.6. Inverse Transformasi Laplace
121
Contoh 59:
122
7. Tambahan 7.1. Jacobian
123
Contoh 60:
124
7.2. pretty(f)
Contoh 61:
125
7.3. int8, int16, int32, dan int64
126
Contoh 62:
127
7.4. uint8, uint16, uint32, dan uint64
Contoh 63:
128
8.Grafik 8.1. ezplot(f)
129
Contoh 64:
130
Contoh 65:
131
Contoh 66:
132
Contoh 67:
133
8.2. ezsurf
134
Contoh 68:
135
Contoh 69:
136
8.3. ezsurfc
137
Contoh 70:
138
Contoh 71:
139
8.3 ezpolar
140
Contoh 72:
141
Contoh Aplikasi Contoh 73:Optimasi
142
Contoh 74:Rangkaian Listrik
143
144