© 2007 Cisco Systems, Inc. All rights reserved. Cisco Public ITE PC v4.0 Chapter 9 1 User Defined Function
7/27/2019 Dasar Komputer-User defined function.ppt
http://slidepdf.com/reader/full/dasar-komputer-user-defined-functionppt 1/10
© 2007 Cisco Systems, Inc. All rights reserved. Cisco Public
ITE PC v4.0
Chapter 9 1
User Defined Function
7/27/2019 Dasar Komputer-User defined function.ppt
http://slidepdf.com/reader/full/dasar-komputer-user-defined-functionppt 2/10
ITE PC v4.0
Chapter 9 2© 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 l ist
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 thesame name as the function and the file extension is“.m”
7/27/2019 Dasar Komputer-User defined function.ppt
http://slidepdf.com/reader/full/dasar-komputer-user-defined-functionppt 3/10
ITE PC v4.0
Chapter 9 3© 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) : untuk menghasilkan nilai absolut dari x sign(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
7/27/2019 Dasar Komputer-User defined function.ppt
http://slidepdf.com/reader/full/dasar-komputer-user-defined-functionppt 4/10
ITE PC v4.0
Chapter 9 4© 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 menghasilkandata yang diinginkan.
Bentuk penulisan nama fungsi:
Contoh:
7/27/2019 Dasar Komputer-User defined function.ppt
http://slidepdf.com/reader/full/dasar-komputer-user-defined-functionppt 5/10
ITE PC v4.0
Chapter 9 5© 2007 Cisco Systems, Inc. All rights reserved. Cisco Public
Fungsi yang dibuat bernama „testfungsi‟ memiliki tiga
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.
User Defined Function
7/27/2019 Dasar Komputer-User defined function.ppt
http://slidepdf.com/reader/full/dasar-komputer-user-defined-functionppt 6/10
ITE PC v4.0
Chapter 9 6© 2007 Cisco Systems, Inc. All rights reserved. Cisco Public
User Defined Functionfunction 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 Descripotion of Change% ==== ========== ======================% 04/12/08 Harinaldi Original code
% Calculate distancedistance = sqrt((x2-x1).^2 + (y2-y1).^2);
7/27/2019 Dasar Komputer-User defined function.ppt
http://slidepdf.com/reader/full/dasar-komputer-user-defined-functionppt 7/10ITE PC v4.0
Chapter 9 7© 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 Description of change% 04/12/08 Harinaldi 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 datadisp ('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 functionresult = dist2 (xA, yA, xB, yB);
% Write out resultfprintf('The distance between point A and B is %f\n', result);
7/27/2019 Dasar Komputer-User defined function.ppt
http://slidepdf.com/reader/full/dasar-komputer-user-defined-functionppt 8/10ITE PC v4.0
Chapter 9 8© 2007 Cisco Systems, Inc. All rights reserved. Cisco Public
Results
>> test_dist2Calculate the distance between two points:
Enter absisca of point A (xA): 1Enter ordinate of point A (yA): 3Enter absisca of point B (xB): 2Enter ordinate of point B (yB): 4The distance between point A and B is 1.414214
7/27/2019 Dasar Komputer-User defined function.ppt
http://slidepdf.com/reader/full/dasar-komputer-user-defined-functionppt 9/10ITE PC v4.0
Chapter 9 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
7/27/2019 Dasar Komputer-User defined function.ppt
http://slidepdf.com/reader/full/dasar-komputer-user-defined-functionppt 10/10ITE PC v4.0
Chapter 9 10© 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_samplea = 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_sampleBefore sample: a = 2.000000, b = 6.000000 4.000000Inside sample: a = 2.000000, b = 6.000000 4.000000Inside sample: a = 10.000000, b = 60.000000 40.000000
After sample: a = 2.000000, b = 6.000000 4.000000After sample: c = 70.000000