0% found this document useful (0 votes)
10 views12 pages

Chapter 6 - Matlab Programing

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

Chapter 6 - Matlab Programing

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Chapter 6: MATLAB Programs

Contents:
1. MORE TYPES OF USER-DEFINED FUNCTIONS
2. MATLAB PROGRAM ORGANIZATION
(Tổ chức chương trình Matlab)
3. MENU-DRIVEN MODULAR PROGRAM
(Menu điều khiển chương trình)
4. VARIABLE SCOPE
(Phạm vi của biến)
5. DEBUGGING TECHNIQUES
(Kỹ thuật gỡ rối)
Chapter 6: MATLAB Programs
1. USER-DEFINED FUNCTIONS

MATLAB allows users to create their own functions to


perform specific tasks. These user-defined functions extend
the flexibility and modularity of code, making it easier to
organize and reuse computations. A user-defined function
typically takes inputs, performs computations, and returns
outputs.
Chapter 6: MATLAB Programs
1. USER-DEFINED FUNCTIONS Example 1: Basic Function
Returning One Value
1.1. Structure of a User-Defined Function
A user-defined function in MATLAB is stored in an .m
file, and the file name must match the function name. function result=
The general structure of a function includes: squareNumber(x)
result = x^2;
Function header: Contains the reserved word function, end
the output variable(s), the function name, and the input Calling the Function:
argument(s).
Comment section: Describes what the function does
(this is optional but important for readability and y = squareNumber(5)
documentation). disp(y);
Body: Contains the actual computations and
assignments.
End statement: The end statement is used to close the
function, though in most cases it's optional for single-
function .m files.
Chapter 6: MATLAB Programs
1. USER-DEFINED FUNCTIONS Example 2: Function
Returning Multiple Values
1.2. Functions with Multiple Inputs and Outputs
function [area, perimeter]
MATLAB functions can accept multiple inputs and return
multiple outputs. In the function header, outputs are
=
enclosed in square brackets, separated by commas, and rectangleProperties(length,
inputs are listed inside parentheses. width)
area = length * width;
perimeter = 2 * (length +
width);
end
Calling the Function:
[a, p] =
rectangleProperties(5, 3);
disp(['Area: ', num2str(a), ',
Perimeter: ', num2str(p)]);
Chapter 6: MATLAB Programs
1. USER-DEFINED FUNCTIONS Example 3: Function Do
Not Return Values
1.3 . Functions That Do Not Return Values

Some functions in MATLAB perform tasks but do not function


return any values. These functions might print outputs printWelcomeMessage()
to the command window, plot graphs, or modify global disp('Welcome to MATLAB
variables. programming!’);
end
Example 4: Tạo hàm vẽ đồ
thị hàm sin và cos trên
cùng một hệ trục. Gợi ý Đặt
tên hàm là:
function plotSinCos()
Chapter 6: MATLAB Programs
1. USER-DEFINED FUNCTIONS Example 4: Anonymous
Function
1.4. Anonymous Functions

Anonymous functions in MATLAB are short, one-line 1. square = @(x) x^2;


functions that do not require a separate file. These are 2. f = @(x) x*exp(x) -1
useful for simple calculations and can be defined within
scripts or passed as arguments to other functions. Calling the Function:
result = square(6); %
result = 36
disp(result);
result = f(0);
disp(result);
Chapter 6: MATLAB Programs
1. USER-DEFINED FUNCTIONS Example 4: Recursive
Function to Calculate
1.5. Recursive Functions Factorial
A recursive function calls itself in its definition.
Recursion is useful for solving problems that can be function result =
divided into smaller subproblems of the same type. factorialRecursive(n)
if n == 1
result = 1;
else
result = n * factorialRecursive(n -
1);
end
end
Chapter 6: MATLAB Programs
3. MENU-DRIVEN MODULAR PROGRAM

Menu-Driven Modular Program là một loại chương trình máy tính có giao
diện người dùng đơn giản, cho phép người dùng tương tác với chương trình
thông qua một menu. Chương trình này thường được cấu trúc theo kiểu mô-
đun, tức là các chức năng khác nhau được tổ chức thành các hàm riêng biệt,
giúp dễ dàng quản lý, bảo trì và mở rộng chương trình.
Đặc điểm của Menu-Driven Modular Program:
Giao diện người dùng rõ ràng: Chương trình hiển thị một menu với các lựa chọn mà người dùng có
thể chọn. Điều này giúp người dùng biết các chức năng nào có sẵn và dễ dàng tương tác với
chương trình.Chức năng mô-đun:Các chức năng khác nhau (như tính toán, nhập dữ liệu, v.v.) được
định nghĩa trong các hàm riêng biệt. Điều này không chỉ giúp tổ chức mã nguồn một cách hiệu quả
mà còn cho phép tái sử dụng mã và giảm thiểu lỗi.
Vòng lặp liên tục: Chương trình thường chạy trong một vòng lặp, cho phép người dùng chọn nhiều
lần cho đến khi họ quyết định thoát khỏi chương trình.
Chapter 6: MATLAB Programs
Các bước tiến hành:
3. MENU-DRIVEN
Bước 1: Xác định chức năng
MODULAR PROGRAM
Chức năng chính: Các phép toán cơ bản như
cộng, trừ, nhân, chia.
Ví dụ: Viết chương trình
Bước 2: Lập cấu trúc chương trình Giao diện
máy tính bỏ túi, cho phép
menu:
người dùng có thể chọn
Thiết kế một menu cho phép người dùng chọn
thực hiện các phép toán
chức năng.
như cộng, trừ, nhân và
Hàm cho từng chức năng: Xây dựng các hàm
chia.
riêng cho từng phép toán.
Chương trình sẽ hiển thị
Vòng lặp: Tạo vòng lặp để chương trình tiếp tục
menu với các tùy chọn này,
chạy cho đến khi người dùng quyết định thoát.
sau đó gọi các hàm tương
Bước 3: Viết mã
ứng để thực hiện các phép Bước 4: Kiểm tra và gỡ lỗi
toán khi người dùng chọn. Chạy chương trình để kiểm tra tính chính xác của các phép toán.
Đảm bảo rằng chương trình xử lý đúng các trường hợp ngoại lệ,
như chia cho 0 hoặc nhập sai dữ liệu.
Bước 5: Tinh chỉnh và mở rộng
Thực hành:

Viết Chương trình Menu-Driven theo yêu cầu sau:


1. Tính giá trị của e^x với một giá trị x cho trước.
2. Tính logarit tự nhiên (ln) của một số.
3. Tính giai thừa (factorial) của một số.
4. Tính giá trị của chuỗi số hạng của e (sử dụng công thức chuỗi Taylor).
5. Thoát khỏi chương trình.

You might also like