MATLAB LECTURE - Trang 22

9/16/2015

3

Soạn thảo Script file trong Matlab

9

function

V =

volume

(r)


%r =

input

('Hay cho ban kinh cua hinh cau')

vol = (4/3)*pi*r^3;

disp

('The tich hinh cau nay la:')

disp(vol

)

Ví dụ về chương trình không có biến vào

Save với tên file

volume

.m, và gọi hàm trong command

windows

>> volume

Soạn thảo Script file trong Matlab

10

function

vol =

volumef

(r)

vol = (4/3)*pi*r^3;

Ví dụ về chương trình không có biến vào

Save với tên file

volumef.m

, và gọi hàm trong command

windows

>>

volumef

(2)

function

V =

Tong_volume

(r1, r2)

V1 = volumef(r1);
V2 = volumef(r2);
V = V1 + V2;

>>

Tong_volume

(2, 5)

Soạn thảo Script file trong Matlab

11

r1 =

input

('Hay cho ban kinh cua hinh cau')

r2 =

input

('Hay cho ban kinh cua hinh cau')

disp(‘Tong the tich hai hinh cau’)
Tong_volume(r1, r2)

Ví dụ về script file

Save với tên file TinhV.m, và gọi hàm trong command

windows

>> tinhV

Soạn thảo Script file trong Matlab

12

• Các m-file dạng hàm

function

[output 1, output 2] =

function_name

(input1,input2)

% Some comments that explain what the function does go here.

global

x y z

persistent

a b c


MATLAB command 1;
MATLAB command 2;
MATLAB command 3;

output 1

= ….;

output 2

= ….;