You are on page 1of 10

User Defined Function

ITE PC v4.0 Chapter 9

2007 Cisco Systems, Inc. All rights reserved.

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)

MATLAB function should be place in a file with the


ITE PC v4.0 Chapter 9

same name as the function and the file extension is .m


2007 Cisco Systems, Inc. All rights reserved. Cisco Public

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

User Defined Function


Di dalam M File, kita dapat menuliskan fungsi-fungsi

yang berisikan berbagai operasi sehingga menghasilkan data yang diinginkan. Bentuk penulisan nama fungsi:

Contoh:

ITE PC v4.0 Chapter 9

2007 Cisco Systems, Inc. All rights reserved.

Cisco Public

User Defined Function


nilai masukan c,d,e dan dua nilai keluaran a,b Selanjutnya fungsi tersebut akan dijalankan melalui command window dengan nilai masukan 10,2,4. Perhatikan penulisan kurung siku [ ] pada nilai keluaran dan kurung biasa( ) pada nilai masukan.
Fungsi yang dibuat bernama testfungsi memiliki tiga

ITE PC v4.0 Chapter 9

2007 Cisco Systems, Inc. All rights reserved.

Cisco Public

User Defined Function


function distance = dist2 (x1, y1, x2, y2) %DIST2 Calculate the distance between two points % Function DIST2 calculates the distance betwee % two points (x1,y1) and (x2,y2) in a cartesian % coordinate system % % Calling sequence: % distance = dist2(x1, y1, x2, y2) % Define variables: % x1 -- absisca position of point 1 % y1 -- ordinate position of point 1 % x2 -- absisca position of point 2 % y2 -- ordinate position of point 2 % distance -- Distance between the two points % Record of revision % Date Programmer % ==== ========== % 04/12/08 Harinaldi

Descripotion of Change ====================== Original code

% Calculate distance distance = sqrt((x2-x1).^2 + (y2-y1).^2);


ITE PC v4.0 Chapter 9 2007 Cisco Systems, Inc. All rights reserved. Cisco Public

Function Caller Script


% % % % % % % % % % % % % % % Script file: test_dist2.m Purpose: This program tests function dist2

Record of revisions: Date Programmer 04/12/08 Harinaldi

Description of change Original code

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

ITE PC v4.0 Chapter 9

2007 Cisco Systems, Inc. All rights reserved.

Cisco Public

Variable Passing: Pass-By-Value


MATLAB programs communicate with their function

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

ITE PC v4.0 Chapter 9

2007 Cisco Systems, Inc. All rights reserved.

Cisco Public

Variable Passing: Pass-By-Value


function out = sample (a, b) fprintf('Inside sample: a = %f, b = %f %f\n', a, b); a = b(1)+ 2*a; b = a .* b; out = a + b(1); fprintf('Inside sample: a = %f, b = %f %f\n', a, b); % Script : test_sample a = 2; b = [6 4]; fprintf('Before sample: a = %f, b = %f %f\n', a, b); c = sample(a,b); fprintf('After sample: a = %f, b = %f %f\n', a, b); fprintf('After sample: c = %f\n', c); >> test_sample Before sample: a = 2.000000, b = 6.000000 4.000000 Inside sample: a = 2.000000, b = 6.000000 4.000000 Inside sample: a = 10.000000, b = 60.000000 40.000000 After sample: a = 2.000000, b = 6.000000 4.000000 After sample: c = 70.000000
2007 Cisco Systems, Inc. All rights reserved. Cisco Public

ITE PC v4.0 Chapter 9

10

You might also like