EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign up
Home Data Science Data Science Tutorials Matlab Tutorial Matlab user defined function

Matlab user defined function

Updated March 13, 2023

Matlab user defined function

Introduction to 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.

ADVERTISEMENT
Popular Course in this category
MATLAB Course Bundle - 5 Courses in 1 | 3 Mock Tests

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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):

Matlab user defined function output 1

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

Input 2 (Calling the function):

Matlab user defined function output 2

Output:

Matlab user defined function output 3

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):

Matlab user defined function output 4

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

Input 2 (Calling the function):

output 5

Output:

output 6

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):

output 7

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

Input 2 (Calling the function):

output 8

Output:

output 9 

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

Conclusion

  1. We can create user defined functions in MATLAB to use them as built-in functions
  2. These user defined functions are stored as text files and can be called whenever required
  3. The name of these text files must be the same as the name of our function

Recommended Articles

This is a guide to Matlab user defined function. Here we discuss the examples of Matlab user defined function along with the inputs and outputs. You may also have a look at the following articles to learn more –

  1. Matlab Mod
  2. Matlab Backslash
  3. Matlab Unit Step Function
  4. Matlab polyfit()
ADVERTISEMENT
SPSS Course Bundle - 14 Courses in 1 | 5 Mock Tests
34+ Hours of HD Videos
14 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
63+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
HADOOP Course Bundle - 32 Courses in 1 | 4 Mock Tests
125+ Hour of HD Videos
32 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
INFORMATICA Course Bundle - 7 Courses in 1
47+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
  • Blog as Guest
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Cyber Monday Reloaded Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW