Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,43 @@
# tutorial-matlab
Pengenalan Bahasa Pemrograman Matlab
# **TUTORIAL MATLAB**
## Pengenalan Bahasa Pemrograman Matlab

Repository ini berisi tutorial yang berisi perintah-perintah mendasar bahasa pemograman Matlab.

<details>
<summary>Fundamental</summary>

- [Membuat `Hello World`](https://github.com/adh182/tutorial_matlab/blob/main/tutorial/hello_world.m)
- [Meminta `input`](https://github.com/adh182/tutorial_matlab/blob/main/tutorial/input_.m)
- [Membuat output dengan menggunakan `disp`](https://github.com/adh182/tutorial_matlab/blob/main/tutorial/disp_.m)
- [Membuat matriks/array](https://github.com/adh182/tutorial_matlab/blob/main/tutorial/matriks.m)
- [Contoh lain membuat matriks](https://github.com/adh182/tutorial_matlab/blob/main/tutorial/matriks2.m)
- [Mendefinisikan sebuah function](https://github.com/adh182/tutorial_matlab/blob/main/tutorial/function_.m)
- [Menghitung luas lingkaran](https://github.com/adh182/tutorial_matlab/blob/main/tutorial/luaslingkaran.m)
- [Menghitung luas persegi](https://github.com/adh182/tutorial_matlab/blob/main/tutorial/luaspersegi.m)

</details>

<details>
<summary>Logika Kondisi</summary>

- [Kondisi `if`](https://github.com/adh182/tutorial_matlab/blob/main/tutorial/ifStatement.m)

</details>

<details>
<summary>Perulangan</summary>

- [Perulangan `while`](https://github.com/adh182/tutorial_matlab/blob/main/tutorial/whileLoop.m)
- [Perulangan `for`](https://github.com/adh182/tutorial_matlab/blob/main/tutorial/forLoop.m)

</details>

<details>
<summary>Advance</summary>

- [Membuat plot](https://github.com/adh182/tutorial_matlab/blob/main/tutorial/Plot.m)
- [Subplot](https://github.com/adh182/tutorial_matlab/blob/main/tutorial/Subplot.m)
- [Penggunaan `hold`](https://github.com/adh182/tutorial_matlab/blob/main/tutorial/holdOnOff.m)
- [Control flow](https://github.com/adh182/tutorial_matlab/blob/main/tutorial/controlFlow.m)

</details>
23 changes: 23 additions & 0 deletions tutorial/Plot.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
clear;clc

x1 = linspace(0, 2*pi, 100); %linspace(start, stop, jumlah titik)
y1 = sin(x1);
x2 = log(x1);
y2 = cos(x1);
x3 = tan(x1);

%plot dengan single line
figure(1)
plot(x1, y1)

%plot dengan multi line
figure(2)
plot(x1, y1, x2, y2)

%plot dengan warna dan line style custom
figure(3)
plot(x1, y1, 'b-', x1, y2, 'ro', x1, x3, 'gx', 'lineWidth',2)
title('GRAFIK SIN COS TAN')
xlabel('X')
ylabel('Y')
axis([0 6 -1 1]) %axis(xmin xmax ymin ymax
27 changes: 27 additions & 0 deletions tutorial/Subplot.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
clear;clc

%subplot

x = linspace(0, 2*pi, 100);
y = sin(x);
y2 = sin(2*x);
y3 = cos(x);
y4 = cos(2*x);


%subplot(baris, kolom, grafik ke-n)
subplot(2,2,1)
plot(x,y)
title('Sin x')

subplot(2,2,2)
plot(x,y2, 'k')
title('Sin 2x')

subplot(2,2,3)
plot(x,y3, 'g')
title('Cos x')

subplot(2,2,4)
plot(x, y4, 'r')
title('Cos 2x')
15 changes: 15 additions & 0 deletions tutorial/controlFlow.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
clear;clc

%pause, break dan continue

for i = 1:30
if i == 19
disp('This is nineteen')
% break %menghentikan looping -- keluar dari looping
continue %melanjutkan looping -- mloncat ke atas,
%tanpa mengeksekusi yg bawah
end
pause(0.25) %menunda looping selama n detik
disp(i)
end
disp('Program ended');
22 changes: 22 additions & 0 deletions tutorial/disp_.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
clear;clc;

%Perintah print/disp --> comment
%Menampilkan sesuatu pada layar

disp('Hello World');
disp('Ini hari Senin')

ilmuwan = 'Isaac Newton'; %variabel_name = value
umur = 40;

disp(ilmuwan) %menampilkan isi dari variabel ilmuwan
disp(umur) %menampilkan isi dari variabel umur

x = 20;
y = 10;
z = 24;

op = x/(y-z);
disp(op)


12 changes: 12 additions & 0 deletions tutorial/forLoop.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
% perulangan for
% for variabel = start:step:stop
% assignment/argument
% end

for i = 10:-1:1 %i dari 1 sampe 10 dengan step sebesar 1
disp(i); %menampilkan i
end

for j = 1:100
disp(j)
end
7 changes: 7 additions & 0 deletions tutorial/function_.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
%function/fungsi
% f(x) = cos (x)

g = @(x) (cos(x)); %fungsi g terhadap x

hasil = g(2*pi); % hasil g untuk x = pi
disp(hasil) %menampilkan hasil
2 changes: 2 additions & 0 deletions tutorial/hello_world.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
disp('Hello Wolrd')
disp(100)
19 changes: 19 additions & 0 deletions tutorial/holdOnOff.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
clear;clc

%hold on hold off

x = linspace(0, 2*pi, 100);
y1 = sin(x);
y2 = cos(x);
y3 = sin(2*x);
y4 = cos(2*x);

plot(x, y1, x, y2)
title('Grafik sin dan cos')
xlabel('Nilai x')
ylabel('Nilai y')
hold on %-- menunggu grafik berikutnya untuk ditimpa

plot(x,y3, 'Color', [0 0.7 0.6])
plot(x,y4, 'k')
hold off %--berhenti menunggu
19 changes: 19 additions & 0 deletions tutorial/ifStatement.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
%percabangan if/elseif/else
% if kondisi
% assigment
% elseif kondisi
% assignment
% else
% assignement
% end


for i = 1:1:15
if i < 5 %logika if
disp(sprintf('i = %d, Nilai i kurang dari 5', i));
elseif i == 5 %logika elseif
disp(sprintf('i = %d, Nilai i adalah 5', i));
else %logika else
disp(sprintf('i = %d, Nilai i lebih dari 5', i));
end
end
8 changes: 8 additions & 0 deletions tutorial/input_.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
%Meminta input/masukan

angka = input('Masukkan angka='); %meminta input dari pengguna
disp(sprintf('Bilangan yang anda pilih adalah %d', angka));
disp(sprintf('Bilangan yang anda pilih adalah %.10f', angka));



10 changes: 10 additions & 0 deletions tutorial/luaslingkaran.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
%fungsi

% function namafungsi(parameter) --> namafungsi harus sama dg nama file
% assignement
% end

function luaslingkaran(r)
luas = pi*r^2;
fprintf('luas lingkaran adalah %.3f \n', luas) %menampilkan luas lingkaran
end
9 changes: 9 additions & 0 deletions tutorial/luaspersegi.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%fungsi
%function nama_variabel = nama_fungsi(parameter)
% assignment
%end

function luas = luaspersegi(p,l)
luas = p*l;
end

41 changes: 41 additions & 0 deletions tutorial/matriks.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
%Jenis-jenis skalar, vektor, matriks
clear; clc;
%skalar
a = 8;

%vektor
%vektor baris
b = [1 2 3 4 5];

%vektor kolom
c = [1; 2; 3; 4; 5];

%matriks
d = [1 2; 3 4];

%OPERASI MATRIKS

%transpose matriks
e = c';

%penjumlahan vektor
f = b + e; %dimensi vektor harus sama

%perkalian vektor

%dot product
g = dot(b,c);

%cross product
%panjang yang dicross kan harus 3, baris atau kolomnya

h = [1 3 4];
i = [5; 7; 9];
j = cross(h,i);

%perkalian matriks
k = [56 66; 12 23];
l = k*d;

%perkalian matriks korespondensi satu-satu
m = k.*d; %perkalian masing-masing elemen matriks
13 changes: 13 additions & 0 deletions tutorial/matriks2.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
clear;clc;

A = [ 23 55; 67 12];
B = [ 1 6; -5 7];
C = A*B;

%X*A = C
%berapa nilai x? seharusnya B, dapat diperoleh dengan left division
X = A\C;

%B*Y = C
%berapa nilai y? seharusnya A, dapat diperoleh dengan right division
Y = C/B;
12 changes: 12 additions & 0 deletions tutorial/whileLoop.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
% perulangan while
% inisialisasi variabel = nilai
% while (kondisi)
% assigment
% increment
% end

i = 0; %inisialisasi nilai i
while (i <=10) %while akan dieksekusi jika kondisi True
disp(i) %menampilkan i
i = i + 1; %menambah nilai i
end