Professional Documents
Culture Documents
Cisco Public
MATLAB Function
A special type of M-file that runs in its own
independent workspace. Receives input data through an input argument list Return results to the caller through an output argument list Bentuk dasar MATLAB function
Function [outarg1, outarg2,]=fname(inarg1, inarg2, ) % H1 comment line .... (Executable code) .... (return) (end)
Built-in-Function
Beberapa fungsi matematika yang dapat kita gunakan untuk operasi matematika antara lain sebagai berikut:
abs(x) sign(x)
: untuk menghasilkan nilai absolut dari x : untuk menghasilkan nilai -1 jika x<0, 0 jika x=0 dan 1 jika x>1 exp(x) : untuk menghasilkan nilai eksponensian natural, e x log(x) : untuk menghasilkan nilai logaritma natural x, ln x log10(x) : untuk menghasilkan nilai logaritma dengan basis 10, x 10 log sqrt(x) : untuk menghasilkan akar dari nilai x, x rem(x,y) : untuk menghasilkan nilai modulus (sisa pembagian) x terhadap y
Untuk selengkapnya fungsi-fungsi lain dapat ditemukan pada buku-buku teks mengenai MATLAB
ITE PC v4.0 Chapter 9 2007 Cisco Systems, Inc. All rights reserved. Cisco Public
yang berisikan berbagai operasi sehingga menghasilkan data yang diinginkan. Bentuk penulisan nama fungsi:
Contoh:
Cisco Public
Cisco Public
Define variables: xA -- absisca position of point A yA -- ordinate position of point A xB -- absisca position of point B yB -- ordinate position of point B result -- Distance between the points
% Get input data disp ('Calculate the distance between two points: '); xA = input ('Enter absisca of point A (xA): '); yA = input ('Enter ordinate of point A (yA): '); xB = input ('Enter absisca of point B (xB): '); yB = input ('Enter ordinate of point B (yB): '); % Evaluation function result = dist2 (xA, yA, xB, yB);
% Write out result fprintf('The distance between point A and B is %f\n', result); ITE PC v4.0
Chapter 9 2007 Cisco Systems, Inc. All rights reserved. Cisco Public
Results
>> test_dist2 Calculate the distance between two points: Enter absisca of point A (xA): 1 Enter ordinate of point A (yA): 3 Enter absisca of point B (xB): 2 Enter ordinate of point B (yB): 4 The distance between point A and B is 1.414214
Cisco Public
using a pass-by-value scheme When a function call occurs, MATLAB makes copy of the actual argument and passes them to the function Even if the function modifies the input arguments, it will not affects the original data in the caller
Cisco Public
10