Categories

# MATLAB user-defined function

A user-defined function in MATLAB is a piece of code or a program that we can create and use later as any other in-built function. All we need to do is save our code as a text file and ensuring that the name of our function is the same as the file we are saving it in.

Functions usually have input arguments and output variables which can be matrices, vectors, or scalars. We can have ‘n’ number of input & output parameters based on our requirement.

## Syntax to create a user-defined function:

``function [o1, o2, o3…, on] = func_name (i1, i2, i3, …,im)``

### Description of the syntax:

1. func_name is the name of our function
2. o1, o2, o3, …, on are the outputs of our function
3. i1, i2, i3, …, im are the inputs of our function

Please note that the func_name is required, whereas there can be zero input & output arguments.

Also, the name of the function defined by us and the name of the file we save it in must be the same.

## Example of Matlab user-defined function

Let us now understand the code to create a user-defined function in MATLAB

## Example 1:

In this example, we will create a user-defined function to calculate the factorial of a number. We will name our function as compute_factorial, and so our file name will also be compute_factorial. Below are the steps to be followed:

1. Initialize the function compute_factorial
2. Write the logic to compute the factorial of a number
3. Save the file by the name compute_factorial
4. Call this function in the new Matlab window using the name and passing the argument

## Code:

``````function fx = compute_factorial (n)
[Initializing the function and naming it compute_factorial] fx = 1;
i = 1;
while i <= n
fx = fx * i;
i = i + 1;
end
end
[Writing the logic to compute the factorial of a number] Next, we will call this function from a new MATLAB window
compute_factorial (6)
[Passing the input argument as 6 to the compute_factorial function]``````

This is how our input and output will look like in MATLAB:

## Input 1 (Creating the function):

(Please note that the name of the file is the same as the name of the user-defined function)

## Output:

As we can see in the output, our user-defined function has given us the factorial of 6, i.e. 720.

## Example 2:

In this example, we will create a user-defined function to calculate the area of a circle. We will name our function as compute_area, and so our file name will also be compute_area. Below are the steps to be followed:

1. Initialize the function compute_area
2. Write the logic to compute the area of a circle
3. Save the file by the name compute_area
4. Call this function in the new Matlab window using the name and passing the argument

## Code:

``````function Area = compute_area (rad)
[Initializing the function and naming it compute_area. Here ‘rad’ signifies the radius of the circle whose area we want to compute] Area = pi*(rad.^2)
[Writing the logic to compute the area of a circle] Next, we will call this function from a new MATLAB command window
compute_area (5)
[Passing the input argument as 5 to the compute_area function]``````

This is how our input and output will look like in MATLAB:

## Input 1 (Creating the function):

(Please note that the name of the file is the same as the name of the user-defined function)

## Output:

As we can see in the output, our user-defined function has given us the area of a circle with a radius of 5, i.e. 78.5398

## Example 3:

In this example, we will create a user defined function to calculate the volume of a sphere. We will name our function as compute_volume, and so our file name will also be compute_volume. Below are the steps to be followed:

1. Initialize the function compute_volume
2. Write the logic to compute the volume of a sphere
3. Save the file by the name compute_volume
4. Call this function in the new Matlab window using the name and passing the argument

## Code:

``````function Volume = compute_volume (rad)
[Initializing the function and naming it compute_volume. Here ‘rad’ signifies the radius of the sphere whose volume we want to compute] Volume = (4/3)* pi*(rad.^3);
[Writing the logic to compute the volume of a sphere] Next, we will call this function from a new MATLAB command window
compute_volume (4)
[Passing the input argument as 4 to the compute_volume function]``````

This is how our input and output will look like in MATLAB:

## Input 1 (Creating the function):

(Please note that the name of the file is the same as the name of the user-defined function)

## Output:

As we can see in the output, our user defined function has given us the volume of a sphere with a radius of 4, i.e. 268.0826