Anda di halaman 1dari 7

# 1.

Pengertian Matlab

## MATLAB (Matrix Laboratory) adalah sebuah program untuk analisis dan

komputasi numerik yang merupakan bahasa pemrograman matematika lanjutan
dengan dasar pemikiran menggunakan sifat dan bentuk matrik. Awalnya merupakan
interface untuk koleksi rutin-rutin numerik LINPACK dan EISPACK yang
menggunakan FORTRAN. Sekarang menjadi produk komersial Mathworks Inc. yang
menggunakan C++.

## MATLAB sering digunakan untuk :

 Matematika dan komputasi
 Pengembangan dan algoritma
 Pemrograman modeling, simulasi dan pembuatan prototipe
 Analisa data, eksplorasi dan visualisasi
 Analisis numerik dan statistik
 Pengembangan aplikasi teknik

## So far we have treated Matlab like a super calculator. However, every

command and expression we have used so far can be used in a Matlab program. In
this sense the commands and expressions of Matlab can be regarded as a
programming language.
Matlab has all the constructs you would expect in a programming language,
such as loops, functions and conditional execution. These programs are stored in files
with a ".m" suffix. For this reason they are general called M files. There are two types
of M files, scripts and functions.

Script M Files

## A Matlab script is a file containing a list of Matlab commands. The commands

in the script are executed by entering the name of the file without the ".m" suffix. The
easiest way to create an M file is to use the editor. Try the following. At the matlab
prompt enter
>> edit myscript.m

## In the editor, enter

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
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.

## % corner(x) returns a two by two matrix containing the values

% in each of the four corners of x.

## [n,m]=size(x); % The number of rows and columns of x

y = [ x(1,1) x(1,m) ; x(n,1) x(n,m) ];

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 :-

The variable "s" contains the sum, and "d" the difference.

## 3.1 Beberapa Bagian dari Window Matlab

 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

 Command History

Window ini berfungsi untuk menyimpan perintah-perintah apa saja yang sebelumnya

 Command Window

menjalankan fungsi, mendeklarasikan variable, menjalankan proses-proses , serta
melihat isi variable.

 Workspace

## Workspace berfungsi untuk menampilkan seluruh variabel-variabel yang sedang aktif

pada saat pemakaian matlab. Apabila variabel berupa data matriks berukuran besar
maka user dapat melihat isi dari seluruh data dengan melakukan double klik pada
variabel tersebut. Matlab secara otomatis akan menampilkan window “array editor”
yang berisikan data pada setiap variabel yang dipilih user.

## 3.2 Getting Help

Matlab menyediakan fungsi help yang tidak berisikan tutorial lengkap mengenai
Matlab dan segala keunggulannya. User dapat menjalankan fungsi ini dengan
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

Matlab hanya memiliki dua jenis tipe data yaitu Numeric dan String. Dalam
matlab setiap variabel akan disimpan dalam bentuk matrik. User dapat langsung
command window. Contoh pembuatam variabel pada Matlab :

varA =
1000

varB =

45 2 35 45

varC =
test variabel

## Penamaan variabel pada matlab bersifat caseSensitif karena itu perlu

diperhatikan penggunaan huruf besar dan kecil pada penamaan variabel. Apabila
terdapat variabel lama dengan nama yang sama maka matlab secara otomatis akan
me-replace variabel lama tersebut dengan variabel baru yang dibuat user.

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

## The basic data element in Matlab is a matrix. A scalar in Matlab is a 1x1

matrix, and a vector is a 1xn (or nx1) matrix.
For example, create a 3x3 matrix A that has 1’s in the first row, 2’s in the
second row, and 3’s in the third row:

>> 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

## If you don’t want MATLAB to display the result of a command, put a

semicolon at the end:

>> 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!

1

## Suppose you want to access a particular row of A:

>> 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:

## >> A(2,1:2) ans =

2 2
You can also use it to create a vector:
>> y = 1:3
y=

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.

## You can easily concatenate vectors and matrices in MATLAB:

>> [ y,

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));

## Untuk membangkitkan suatu "ramp" dari 1 sampai 50 coba:

>> x = [1:1:50];

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];