How to Create a Function in MATLAB?


In this article, we will learn how to create a function in MATLAB and how to use that function in a MATLAB program to perform an operation.

What is a Function in MATLAB?

In MATLAB, a function is nothing but a block or set of instructions used to perform a specific operation. A function basically eliminates the need of writing instructions repeatedly. Also, it encapsulates all the technical information of the operations and provides a short and easy to use executable code to perform the operation. Functions has several advantages such as easy implementation, reusability, easy maintenance, etc.

Components of a Function

In MATLAB, a function has the following main components:

  • Function Signature − In MATLAB, the function signature is the code written in the first line of the function declaration. It contains the keyword ‘function’, function name, and a list of input and output arguments.

  • Input Arguments − It is a list of variables specified inside the parenthesis of a function declaration.

  • Function Body − In MATLAB, the function body starts after the function signature line. It has the code defining the operation that the function will perform.

  • Function End − In MATLAB, a function is ended by using the ‘end’ keyword.

Syntax of Function in MATLAB

The function declaration in MATLAB takes the following syntax:

% Function signature
function output_variable = function_name(list of input variables separated by a comma)
% Function body
output_variable = operation;
end

Steps to Create a Function in MATLAB

The step−by−step procedure to create a function in MATLAB is explained below:

Step (1) − Declare a function:

In MATLAB, we use the ‘function’ keyword to declare a function. Function declaration includes specifying the output variable, function name, and input arguments. This line is called function signature.

Step (2) − Specify a list of input arguments:

In this step, we specify a list of input variables as the function arguments. This list is specified inside a parenthesis ‘()’ next to the function name.

Step (3) − Define an operation to perform on input variables:

This step defines the body section of the function. Here, we specify a mathematical expression using input variables and operators to perform a specific operation or computation.

Step (4) − End the function:

Finally, we end the function by using the ‘end’ keyword.

Now, let us consider a few MATLAB examples to practically understand how to create a function in MATLAB.

Create a MATLAB function to calculate sum of three numbers

Consider we have three numbers ‘A’, ‘B’, and ‘C’. We have to create a function in MATLAB to perform sum of three numbers. The following example demonstrates the implementation of this statement.

Example

% MATLAB code to create a function
% Create a function to calculate sum of three numbers
function sum_result = calSum (A, B, C)
	sum_result = A + B + C;
end

Now, save this function code in a file with extension ‘.m’. After that we can call this function from MATLAB command window using the function name ‘calSum’.

The following code show how to use the above function ‘calSum’ to calculate the sum of three numbers.

% MATLAB code to call a function
% Initialize the input arguments
A = 100;
B = 50;
C = 150;

% Call the function ‘calSum’ to calculate the sum of A, B, and C
Result = calSum(A, B, C);

% Display the result
disp('The sum of A, B, and C is:');
disp(Result);

Output

The sum of A, B, and C is:
   300

Create a function in MATLAB to calculate simple interest

The simple interest is calculated by using the principal amount (P), rate of interest (R), and time period (T). The following formula is used to compute the simple interest,

SI = (P * R * T) / 100

The following MATLAB example demonstrates the creation of a function to calculate simple interest.

Example

% MATLAB code to create a function to calculate simple interest
function SI = CalSI(P, R, T)
	SI = (P * R * T) / 100;
	end

Save the above code in a file with ‘.m’ extension. Once, you save this function, you can call it in the MATLAB command window by its name. The following code shows how to call the above function.

% MATLAB code to call the simple interest function
% Provide value of P, R, and T
P = 10000;	% Principal amount 
R = 10;	% Rate of interest
T = 2;		% Time period in years

% Call the function ‘CalSI’ to compute the simple interest
Interest_Amount = CalSI(P, R, T);

% Display the result
disp('The simple interest is:');
disp(Interest_Amount);

Output

The simple interest is:
        2000

Conclusion

In conclusion, the process of creating a function in MATLAB is quite straightforward. In MATLAB, when a block of code is need to execute repeatedly, then we can define a function for this block code. This makes the use of code in the program easier and it also encapsulates (hide) the complex instruction statements, making the program simple to write and maintain.

Updated on: 08-Aug-2023

315 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements