Professional Documents
Culture Documents
Pengertian Matlab
Script M Files
x = linspace(0,2*pi,100);
y = sin(x);
plot(x,y);
Then save. At the matlab prompt enter the name of the script to run it.
>> myscript
You can add your own comments to the script. Place a percent sign (%) before
each comment. Everything after the percent sign on that line is regarded as a
comment. Comments are displayed in green in the editor.
Function M Files
If an M file has the word "function" on the first line, then the file contains a definition
of a MATLAB function. Here is a listing of a file called corner.m.
function y = corner(x)
% CORNER the corners of a matrix.
The first line specifies that the function name is "corner" and that "x" is an
input parameter and "y" is the return value. The next three lines are comments. These
lines are used by the help command, they will be printed out if you type "help corner".
The rest of the file contains the commands to implement the function. The last
line assigns a value to "y" which is returned. The variable "n" and "m" are local to the
function. They cannot be accessed outside the function. This function can now be
used in an expression in the same way as any other function.
>> corner(p)
>> c = corner(w);
>> help corner
You may have noticed that we used a function called size. This returned the number
of rows and the number of columns of the matrix x. Unlike some programming
languages, MATLAB functions can return more that one item. The following example
shows how you would write a function that returns many items.
sum = a + b;
difference = a - b;
To call this function, you would enter :-
>> [s d] = add_subtract(1,2)
The variable "s" contains the sum, and "d" the difference.
Current Directory
Window ini menampilkan isi dari direktori kerja saat menggunakan matlab. Kita
dapat mengganti direktori ini sesuai dengan tempat direktori kerja yang diinginkan.
Default dari alamat direktori berada dalam folder works tempat program files Matlab
berada.
Command History
Window ini berfungsi untuk menyimpan perintah-perintah apa saja yang sebelumnya
dilakukan oleh pengguna terhadap matlab.
Command Window
Window ini adalah window utama dari Matlab. Disini adalah tempat untuk
menjalankan fungsi, mendeklarasikan variable, menjalankan proses-proses , serta
melihat isi variable.
Workspace
Matlab menyediakan fungsi help yang tidak berisikan tutorial lengkap mengenai
Matlab dan segala keunggulannya. User dapat menjalankan fungsi ini dengan
menekan tombol pada toolbar atau menulis perintah ‘helpwin’ pada command
window. Matlab juga menyediakan fungsi demos yang berisikan video tutorial matlab
serta contoh-contoh program yang bisa dibuat dengan matlab.
2.3 Interupting dan Terminating dalam Matlab
Untuk menghentikan proses yang sedang berjalan pada matlab dapat dilakukan
dengan menekan tombol Ctrl-C. Sedangkan untuk keluar dari matlab dapat dilakukan
dengan menuliskan perintah exit atau quit pada comamnd window atau dengan
menekan menu exit pada bagian menu file dari menu bar.
Matlab hanya memiliki dua jenis tipe data yaitu Numeric dan String. Dalam
matlab setiap variabel akan disimpan dalam bentuk matrik. User dapat langsung
menuliskan variabel baru tanpa harus mendeklarasikannya terlebih dahulu pada
command window. Contoh pembuatam variabel pada Matlab :
45 2 35 45
5. Matrices
The fundamental unit of matlab is a matrix. In fact matlab is short for matrix
laboratory. However, its use is not restricted to matrix mathematics. Matrices in
matlab can also be regarded as arrays of numbers. You can regard matrices as a
convenient way of handling groups of numbers. A matrix in matlab can have one, two
or more dimensions or be empty.
A matrix with a single element is a special case. They are treated as a single
number. A matrix element can be an integer, a real or a complex number. You do not
have to worry about element types. Matlab will set the element type to what is
required. Matrices that contain a single row or Column are called vectors.
Creating matrices
>> A = [1 1 1; 2 2 2; 3 3 3]
The semicolon is used here to separate rows in the matrix. Matlab gives you:
A=
1 1 1
2 2 2
3 3 3
>> A = [1 1 1; 2 2 2; 3 3 3];
Matrix A has been created but MATLAB doesn’t display it. The semicolon is
necessary when you’re running long scripts and don’t want everything written out to
the screen!
>> A(2,:)
ans =
2 2 2
The “:” operator you have just used generates equally spaced vectors. You can use it
to specify a range of values to access in the matrix:
1 2 3
The default increment is 1, but you can specify the increment to be something else:
>> y = 1:2:6
y=
1 3 5
Here, the value of each vector element has been increased by 2, starting from 1,
while less than 6.
A(2,:)]
ans =
1 3 5 2 2 2
You can also easily delete matrix elements. Suppose you want to delete the 2nd
element of the vector y:
>> y(2) = []
y=
1 5
MATLAB has several built-in matrices that can be useful. For example,
zeros(n,n) makes an nxn matrix of zeros.
>> B =
zeros(2,2)
>> B =
0 0
0 0
A few other useful matrices are:
zeros – create a matrix of zeros
ones – create a matrix of ones
rand – create a matrix of random numbers
eye – create an identity matrix
6. Pendefinisian Vektor
Suatu vektor 1 kali 100 yang menyusun sample pada sinyal cosinus dapat
dibangkitkan dengan
>> x = cos(0.1*pi*(0:99));
Bilangan kedua mengindikasikan step kenaikan dari dari 1 sampai 50. Untuk
membangkitkan suatu fungsi "ramp" dari 1 sampai 50 coba berikut ini:
>> x = [1:1:50];
Ketika anda tidak memasukkan angka kedua pada perintah diatas, maka secara
otomatis (default) step kenaikan ditetapkan bernilai “1”:
>> x = [1:50];
Anda bisa juga secara khusus mendefinisikan suatu rentang nilai pada x sebagai
berikut:
>> x(51:100) = [50:-1:1]
Ini merupakan metode yang sangat bermanfaat untuk mensepsifikasi nilai “waktu”
untuk penggambaran. Sebagai contoh, ditetapkan interval sampling dalam contoh
diatas adalah 1 detik. Selanjutnya anda dapat mendefisnisikan seperti berikut:
>> time = [0:0.001:0.099];