TUTORIAL MATLAB
MATLAB
OUTLINE
1.
2.
3.
4.
5.
If..else
For
While
Do
6. Fungsi:
Eliminasi Gauss
Mencari akar: fzero & roots
SPANL: solve
Persamaan Diferensial Biasa (PDB): ode45
EDITOR:
Function/subroutines
WORKSPACE:
Variabel2 yg sudah
diinput
COMMAND WINDOW:
-Menuliskan perintah2/program utama
- Memasukkan data
- Mengeksekusi program dan keluar hasil
1. INPUT VARIABEL
a vector x = [1 2 5 1]
x =
a matrix
x =
1
3
transpose
x = [1 2 3; 5 1 4; 3 2 -1]
2
2
3
-1
y = x
1
2
5
1
4
y =
1. INPUT VARIABEL
t =1:7
t =
1 2 3 4 5 6 7
k =2:-0.5:0
k =
2 1.5
1 0.5
B = [1:4; 5:8]
x =
1
5
2
6
3
7
4
8
Informasi Tambahan
Agar hasil tdk tampil setelah mengetikkan
perintah pada akhir baris tambahkan tanda(;)
Cth: A=[1 2 3];
Pemisah antar kolom matriks : spasi ( ) atau
koma (,)
Pemisah antar baris matriks: titik koma (;)
Matriks khusus
zeros (M,N) =
Matriks 0 orde MxN
x = zeros(1,3)
x =
0
0
0
ones (M,N) =
Matriks 1 orde MxN
x = ones(1,3)
x =
1
1
1
eye (n) =
Matriks identitas nxn
x =
x =
1
0
0
eye(3)
0
1
0
0
0
1
Matriks khusus
linspace (a,b,n) =
Vektor ruang linier dari a
sampai b dengan jumlah
titik/elemen sebanyak n
(mengikuti baris
aritmatika)
x = linspace(1,10,5)
x =
1
3.25
5.5 7.75
10
2. OPERASI MATRIKS
Simbol/perintah
Jenis operasi
Penjumlahan
Pengurangan
Perkalian
Pembagian
Pangkat
Transpose
.*
Perkalian elemen2
./
Pembagian elemen2
.^
Pangkat elemen2
inv(A)
2
5
8
3
6
9
Perintah
A(1,3)
A(2,:)
Hasil
3
4 5 6
A(:,3)
3
6
9
A(2,1:2)
A(1:2,1:2)
1
4
2
5
A(8)
LATIHAN
Cari Penyelesaian SPAL:
x + 2y z = 10
4y 2z = 15
3x + 2y + z = 8
3. PERINTAH-PERINTAH DASAR
Perintah
Keterangan
clc
clear
whos
save
Menyimpan variabel
(workspace)
4. FLOW CONTROL
if
for
while
do
== Equal to
~= Not equal to
< Strictly smaller
> Strictly greater
<= Smaller than or equal to
>= Greater than equal to
& And operator
| Or operator
Control Structures
If Statement Syntax
if (Condition_1)
Matlab Commands
elseif (Condition_2)
Matlab Commands
elseif (Condition_3)
Matlab Commands
else
Matlab Commands
end
Control Structures
For loop syntax
for i=Index_Array
Matlab Commands
end
Control Structures
While Loop Syntax
while (condition)
Matlab Commands
end
Dummy Example
while ((a>3) & (b==5))
Some Matlab Commands;
end
5. PLOT GRAFIK
x = [0,pi/100,2*pi]; atau
x = linspace(0,2*pi,100);
y = sin(x);
plot(x,y)
LATIHAN
Plot the function e-x/3 sin(x) between 0x4
Jawaban
Create an x-array of 100 samples between 0 and
4.
>>x=linspace(0,4*pi,100);
5. PLOT GRAFIK
Plot y vs x
>>plot(x,y2)
1
0.8
0.6
y 0.4
0.2
0
-0.2
-0.4
0
10
12
6. FUNGSI-FUNGSI
6. FUNGSI-FUNGSI
Mencari akar fungsi
Non-linier: fzero
Syntax: fzero(function,x0)
x0 = tebakan awal
Polynomial: roots
Syntax: roots([matriks koefisien])
Solution #2
>>roots([1 0 -2 -5])
Result:
ans =
2.0946 + 0.0000i
-1.0473 + 1.1359i
-1.0473 - 1.1359i
LATIHAN
6. Fungsi-fungsi
SPANL fsolve
syntax: fsolve(fun,x0)
PDB/ODE ode45
Solusi-Function
function F = myfun(x)
F = [2*x(1) - x(2) - exp(-x(1));
-x(1) + 2*x(2) - exp(-x(2))];
end
x0 = [-5; -5];
options = optimoptions('fsolve','Display','iter');
[x,fval] = fsolve(@myfun,x0,options) % Call
solver
RESULT
Norm of First-order Trust-region
Iteration Func-count f(x)
step
optimality radius
0
3
47071.2
2.29e+04
1
1
6
12003.4
1
5.75e+03
1
2
9
3147.02
1
1.47e+03
1
3
12
854.452
1
388
1
4
15
239.527
1
107
1
5
18
67.0412
1
30.8
1
6
21
16.7042
1
9.05
1
7
24
2.42788
1
2.26
1
8
27
0.032658
0.759511
0.206
2.5
9
30 7.03149e-06
0.111927
0.00294
2.5
10
33 3.29525e-13 0.00169132
6.36e-07
2.5