Matlab merupakan perangkat lunak interaktif untuk melakukan perhitungan numerik dan visualisasi data. Matlab memungkinkan pengguna untuk melakukan operasi matematika kompleks, simulasi, dan pengembangan GUI.
1. MATLAB
(MATrix LABoratory)
I.
Pendahuluan
Matlab adalah singkatan dari Matrix Laboratory. Versi pertama Matlab
ditulis di University of New Mexico dan Stanford University pada akhir tahun 1970an penggunaannya diperuntukkan pada Teori Matriks, Aljabar Linier dan Analisis
Numerik.
Hingga perkembangannya saat ini, Matlab telah dikembangkan dengan
berbagai kelengkapan pada toolbox yang akan memudahkan para engineer dalam
menggunakannya. Berikut beberapa macam penggunaan pada Matlab:
Matematika dan Komputasi
Pengembangan Algoritma
Pemodelan, simulasi dan purwarupa (prototyping)
Analisis Data, eksplorasi dan visualisasi
Scientific dan Engineering Graphics
Pengembangan Aplikasi, membangun GUI (Graphical User Interface).
Matlab merupakan sistem interaktif yang mempunyai elemen basic data
berupa ARRAY. Dengan kata lain, Matlab sama halnya dengan:
-
Kalkulator pada umumnya yang memiliki fungsi matematika sederhana
seperti penambahan, pengurangan, perkalian dan pembagian, namun juga
dapat divisualiasasikan dalam bentuk grafik.
-
Scientific Calculator yang dapat mengerjakan operasi akar pangkat,
bilangan kompleks, logaritma dan trigonometri seperti sinus, kosinus dan
tangent.
-
Programmable Calculator yang dapat digunakan untuk menyimpan dan
memanggil data yang dibuat, eksekusi dan menyimpan sederetan
perintah-perintah juga dapat membuat perbandingan serta control orde
yang memiliki perintah yang akan dieksekusi.
Pada akhirnya, sebuah kalkulator yang powerful seperti MATLAB
memungkinkan untuk mengerjakan aljabar matriks, untuk memanipulasi polynomial
dan menampilkan data hasil eksekusi.
2. Gambar Tampilan Awal MATLAB
MATLAB WORKSPACE
Merupakan area kerja dimana variabel disimpan dan perintah-perintah serta
scripts dieksekusi. Berikut beberapa perintah/commands yang sering digunakan pada
operasi MATLAB:
>>clc % clear the command window
>>clear % clear the workspace contents
>>whos % display the contents of the workspace
>>what % display files in the current folder
>>doc function % display help for Matlab function
>>path % display or modify current Matlab path
>>pause % pause program execution
NOTE: Perlu diingat!
Tanda “%” merupakan penanda untuk komentar pada baris
yang ditandai dan simbol ini tidak akan dieksekusi pada
operasi MATLAB.
Variabel dan Inisialisasi
Variabel MATLAB termasuk arrays dapat berupa real atau complex dan
strings. Ketentuannya sebagai berikut:
-
Arrays diikuti dengan square brackets [ ].
-
Strings diikuti quotes 'String'.
3. -
Elemen dalam array dipisah oleh spasi atau koma.
-
Kurung buka tutup tidak berlaku pada scalars.
-
Tanda titik koma digunakan pada akhir kalimat.
Berikut contoh penggunaan Inisialisasi variabel menggunakan kalimat
penugasan:
>>R = 2.5 % define the radius of a circle
>>A = 2*pi*R; % find area of circle
>>str = 'String' % String character
>>a = 2 + 3i; % complex sclar
>>b = [1 2 3]; % 1 by 3 row vector
>>b = [1;2;3]; % 3 by 1 column vector
>>A = [1 2;2 4]; % 2 by 2 matrix
Variabel – variabel yang diinisialisasi menggunakan operasi MATLAB dan
fungsi MATLAB yang sering digunakan:
>>x = m:n % x=m+1, m+2,…, n
>>x = m:dn:n; % x=m+dm, m+2dm, …,n
>>x = linspace(a,b,n) % n values equally spaced over [a,b]
>>x = input(„x = „) % enter data from the keyboard
>>A = zeros(m,n); % m by n matrix of zeros
>>B = ones(m,n); % m by n matrix of ones
>>C = rand(m,n); % m by n matrix of random numbers
>>N = length(x); % number of elements of x
>>y = H(k,:); % row k of array H = vector y
>>y = H(:,k); % column k of array H = vector y
>>H(k,:) = y; % row k of array H = y
>>H(:,k) = y; % column k of array H = y
Operasi Matematika
Dalam operasi matematika, operasi matriks dan vector yang sering digunakan
adalah Skalar dan Array:
Scalar Operators
>>y = x + a; % scalar add y(j) = x(j) + a
>>y = x – a; % scalar subtract y(j) = x(j) - a
>>y = x*a; % scalar multiply y(j) = a*x(j)
>>y = x/a; % scalar divide
>>y = x^a; % scalar power
4. Array Operators
>>z = x + y; % addition of arrays/vectors
>>z = x - y; % subtraction of arrays/vectors
>>z = x.*y; % multiplication of arrays/vectors
>>z = x./y; % division of arrays/vectors
>>z = x.^y; % array/vector power
>>z = x'; % transpose of array/vector
Operasi Logika dan Perbandingan
MATLAB memiliki koleksi standar relasional dan operator logika, seperti
berikut:
>>a < b; % is a less than b
>>a <= b; % is a less than or equal to b
>>a > b; % is a greater than b
>>a >= b; % is a greater than or equal to b
>>a == b; % is a is equal to b
>>a ~= b; % a not equal b
>>a & b; % a and b
>>a | b; % a or b
Ekspresi logika dengan “if statement” :
if expression
statement 1
else
statement 2
end
Ekspresi logika dengan “switch statement” :
switch choice
case value_1
statement_1
case value_2
statement_2
case value_n
statement_n
otherwise
default case
end
Perulangan (Loops)
Perulangan pada operasi MATLAB:
For k = 1:n:m
statements % for loop body
end
5. “the for statement”
For k = 1:n:m
statements % for loop body
end
“the while loop”
while expression_1
statements % while loop body
end
MATLAB functions
x = cos(a); % cosine of a
x = sin(a); % sine of a
x = tan(a); % tangent of a
a = acos(x); % arc cosine of x
a = asin(x); % arc sine of x
a = atan(x); % arc tangent of x
y = exp(x); % exponential of x
y = log(x); % natural log of x
y = log10(x); % common log of x
y = sqrt(x); % square root of x
z = mod(x,y); % remainder of x/y
n = floor(x); % round x down
n = ceil(x); % round x up
n = round(x); % round x up to nearest integer
n = num2str(x); % convert n to string
x = str2num(n); % convert string to number
y = abs(x); % absolute value
y = angle(x); % angle of complex number x
y = imag(x); % imaginary part of x
y = real(x); % real part of x
y = polyval(a,x); % evaluation of a polynomial
r = roots(d); % roots of a polynomial
y = max(x); % find maximum of x
y = min(x); % find minimum of x
[n,m] = size(A); % dimension of array A
Perintah membuat Grafik pada MATLAB:
figure; % create a new figure window
plot(y) % plot y vs its subscript
plot(x,y); % plot y vs x
stem(y); % discrete plot
stem(x,y) % discrete plot of y vs x
xlabel(„label‟) % insert x axis string
ylabel(„label‟) % insert y axis string
title(„title‟) % insert title string
legend(s1,s2,…,sp) % create figure legend
axis([x1 x2 y1 y2]) % set plot limits for plot
semilogx(y); % plot y vs x as log x
subplot(m,n,p); % create an m by n array of plots
6. Program Operasi Matematika pada Editor MATLAB
Pada materi ini, kita akan mengerjakan operasi matematika pada editor
MATLAB layaknya menggunakan sebuah scientific calculator.
Dalam menulis perintah (commands) pada editor MATLAB dapat mengikuti
langkah berikut:
1. Buka Program MATLAB.
2. Pada editor yang tampil, kita dapat menuliskan beberapa variabel maupun
array yang akan dieksekusi secara langsung dengna menampilkan hasil
saat ditekan ENTER seperti berikut:
Contoh 1.
>> 1 + 1
ans =
2
Contoh 2.
>> a = 5 ; b = 7 ; c = a*b
c =
35
Contoh 3.
>> a = 5 ; b = 7 ; c = a/b
c =
0.7143
3. Tekan clc untuk membersihkan layar editor MATLAB.
7. TUGAS
Buatlah program untuk analisis rangkaian DC dengan menggunakan Mesh
seperti pada gambar berikut:
V1
15Vdc
R1
R2
10
10
V1
R3
5
10Vdc
R4
5
0
>> ' Mesh Current Example'
R1 = 10; R2 = 10; R3 = 5; R4 = 5; % define resistor
values
E1 = 10; E2 = 15; % Voltage source values
Z(1,:) = [R1+R2 -R2 -R1]; % row 1 of Z Matrix
Z(2,:) = [-R2 R2+R3+R4 -R3]; % row 2 of Z Matrix
Z(3,:) = [-R1 -R3 R1+R3]; % row 3 of Z Matrix
E = [E1 0 E2]'; % source vector (note how formed)
I = ZE; % solving for mesh currents
V1 = (I(1)-I(2))*R2; % node voltage V1
V2 = I(2)*R4; % node voltage V2
V = [V1 V2];
% display mesh currents and node voltages
disp('Mesh Currents')
disp(I)
disp('Node Voltages')
disp(V)
Tekan ENTER
ans =
Mesh Current Example
Mesh Currents
4.2500
2.5000
4.1250
Node Voltages
8.7500
25.0000
8. Program M-File MATLAB
Program MATLAB M-File digunakan untuk menulis program atau fungsifungsi yang dapat digunakan dalam memecahkan permasalahan secara bersamaan.
M-file memiliki notasi function.m dan disimpan dalam folder kerja saat menjalankan
program atau dalam folder pada MATLAB path.
Dalam membuat m-file dapat mengikuti langkah berikut:
1. Buka MATLAB.
2. Tekan File – New – Blank M-File
3. Setelah muncul tampilan baru, maka m-file siap digunakan dalam editor
yang baru.
4. Ketikkan script berikut berdasar pada formulasi dibawah ini:
dimana
dan
'Matlab Script'
Pm = 10; a = 0.1;
x = linspace(-10,10,500);
x2 = x.^2;
P = Pm*exp(-a*x2);
plot(x,P); grid
xlabel('Time(sec)'); ylabel('Amplitude');
title('Plot of P(x)=P_me^{-ax^2}')
9. 5. Tekan
untuk menjalankan script yang telah dibuat dan sekaligus
menyimpan berkas yang telah dibuat.
6. Gambar akan ditampilkan pada window baru seperti berikut:
Pada Latihan berikut ini, eksekusi m-file akan dijalankan pada editor
MATLAB dengan cara memanggil function yang telah dibuat pada m-file.
1. Sama seperti langkah sebelumnya,
2. buka editor m-file.
3. Ketikkan script berikut:
function y = f_cosine(A,f0,Fs,t0,IPlot)
Npts = ceil(Fs*t0); % determine number of time
samples
n = 0:Npts-1; % build sample vector
y = A*cos(2*pi*n*f0/Fs); % build time samples
switch IPlot
case 1
stem(n,y); % plot using stem option
xlabel('n') % add x axis label
ylabel('Amplitude') % add y axis label
Amp = num2str(A); % Amplitude part of title
string
% freq part of title string
w0 =
(['2*pi',num2str(f0),'*n/',num2str(Fs)]);
% build plot title, note use of quotes and
strings
11. LATIHAN SOAL
function y = f_sumsines(A,f,Fs,T,IPlot)
% Inputs A and f are 1 by 2 vectors
% A = amplitude and f = frequency
Npts = ceil(Fs*T); % determine number of time samples
n = 0:Npts-1; % build sample vector
y1 = A(1)*cos(2*pi*n*f(1)/Fs);
y2 = A(2)*sin(2*pi*n*f(2)/Fs);
y = y1 + y2; % build time samples
switch IPlot
case 1
subplot(3,1,1);
stem(n,y1,'fill'); % plot y1 using stem option
xlabel('n') % add x axis label
ylabel('Amplitude') % add y axis label
Amp1 = num2str(A(1)); % Amplitude part of title string
% freq part of title string
w1 = (['2*pi',num2str(f(1)),'*n/',num2str(Fs)]);
% build plot title, note use of quotes and strings
title(['y(t)=',Amp1,'cos(',w1,')']);
subplot(3,1,2);
stem(n,y2,'r','fill'); % plot y2 using stem option
xlabel('n') % add x axis label
ylabel('Amplitude') % add y axis label
Amp2 = num2str(A(2)); % Amplitude part of title string
% freq part of title string
w2 = (['2*pi',num2str(f(2)),'*n/',num2str(Fs)]);
% build plot title, note use of quotes and strings
title(['y(t)=',Amp2,'sin(',w2,')']);
subplot(3,1,3);
stem(n,y,'fill','g'); % plot sum using stem option
xlabel('n') % add x axis label
ylabel('Amplitude') % add y axis labe
% build plot title, note use of quotes and strings
title(['y(t)=',Amp1,'cos(',w1,')+',Amp2,'sin(',w2,')']);
otherwise
end
Gambar Hasil Eksekusi