MATLAB for Engineers - Introduction to User-Defined Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video I'm going to introduce you to user-defined functions in MATLAB I'll walk you through two examples and then I'll show you how to pass arguments to and from the functions let's get started Before we jump into MATLAB let's take a look at the structure of a user-defined function the first line in a function file must begin with a function definition line that contains the functions inputs and outputs the output arguments are the variables whose values are calculated by the function using the provided input arguments the output arguments are enclosed in square brackets though the brackets are optional if there is only one output the input arguments are enclosed with parentheses the function name is the same name that the file will be saved as in the comments section while not required is useful for documenting the purpose of the function as well as input and output arguments I would highly recommend including this section particularly if other users may use the function in the future finally an end statement is used to terminate a function while the end statement is occasionally optional the best practice would be always to include it so now let's take a look at the first example that we're going to work in MATLAB so we're going to use the Pythagorean theorem to calculate the hypotenuse of a right triangle when the length of the other two sides are known in this case our function is going to have two inputs and one output the input arguments are going to be the length of side a and the length of side B and the single output argument is going to be the hypotenuse C which we're going to calculate based on the Pythagorean theorem alright let's move into MATLAB and implement this function the first thing I'm going to do is start a new user-defined function file so I'll click new and then function in a template pops up right away now I'd like to have a bit more screen real estate to work with here so I'm going to go ahead and minimize the current folder which shows the directory and I'm also going to minimize the workspace which shows any stored variables okay so our output argument is the hypotenuse that we'll calculate with the Pythagorean theorem and I'm going to use variable C for the hypotenuse our input arguments are the two sides of the right triangle that are going to be used to calculate the hypotenuse and that's a and B our function name I'm going to call Pythagorean and when choosing a function name your main consideration is does this function name already exist in MATLAB to find out you can type exist then your potential function name that you'd like to use and if you get a value of zero returned that indicates that this name is not already being used as a built-in function in MATLAB so you should be good to go to use that name next we're going to go to our comment section and write a brief summary of the function so here is the function name a brief summary uses the Pythagorean theorem and a little bit more detail would be calculates the hypotenuse and that's sufficient for now okay so next we're going to calculate the hypotenuse based on the Pythagorean zero so our hypotenuse C is equal to the square root of a squared plus B squared now I'm going to add a semicolon at the end of this line to suppress the output and we'll go ahead and save this file and you'll notice when you click Save that the function name automatically fills in so I'm good to go here I'll just click Save again okay so now what I want to do is I want to be able to calculate the hypotenuse based on two given side lengths so this is how we would call the function in the command window you would type in the function name and then you enter in your two inputs a and B so let's say side length of a is three and the side length of side B is four and I'm going to click enter here and I get a value of five which is what I'm expecting based on the Pythagorean theorem so this is correct now one thing I want to note is let's go ahead and expand this workspace if your workspace is not already expanded so I'm going to restore it and you'll see that in this workspace I don't have a variable C I have this variable called ans for answer and it does have that value of five and this because unlike tit unlike a script file all the variables in a function file are local variables which means their values are available only within the function itself so what that means is if I actually to store that answer as a variable I would have to tell MATLAB to do that so for example the hypotenuse let's call it variable C again I would type in C is equal to now I would call the function and enter in the side lengths and click enter and great now we have C is equal to 5 and you'll notice that in your workspace this value is actually stored so again a really important consideration when using functions is the fact that those variables are stored locally now let's take a look at a second example the purpose of this function is going to be to calculate the area and volume of a cylinder when the radius and height are known this function is going to have two inputs and two outputs our input arguments are going to be the radius and the height of the cylinder and our output arguments are going to be the area and volume of the cylinder okay let's move to MATLAB so again we're going to start by creating a new function so we'll go to new and then function again I'm going to collapse the current folder and the workspace again to have a little bit more room to work with now in this case we have two output arguments the area and the volume of the cylinder so I'm going to use a four area in v4 volume our input arguments needed to calculate the area and the volume or the radius which I'm going to use R and the height H next I'm going to change the function name from the default value in the template and I'm going to call this function area underscore volume now I'm going to move on to the comment section so we have the function name area underscore volume and then a summary of the function which is going to be calculates the area in volume of a cylinder so next we'll do our calculations first let's start with the area of the cylinder so our area is equal to two times pi times the radius times the height plus two times pi times the radius squared and I'm going to add a semicolon to suppress this from outputting to the command window next I'm going to do the volume calculation and the volume of a cylinder is pi times the radius squared times the height and again I'll add a semicolon here and then our function ends with the end statement so now I'll click Save and the function name is already filled in so I'll click Save again and now I want to run this function so I'm going to provide the function the input arguments of the radius and the height now again if you recall from our previous example function variables are stored locally and so if we want to actually use these variables in the future we need to tell MATLAB to store them so we have two variables that I want to store area and volume and now I'm going to call the function by typing in the function name and providing the two input arguments which are the radius so let's say it's two in the height let's say it's 20 and then we'll click enter and now you'll see that we have two answers one for area and one for volume and if I go to the workspace and I'll restore it we see that okay great we have stored these two variables successfully and they are not just stored locally inside the function now as that answer variable okay that's it I hope this video provided you a good introduction to how to use functions in MATLAB thanks for watching you can find me at david calamus on twitter instagram and linkedin if you like this video and would like to see more subscribe below
Info
Channel: Spartan Professor
Views: 94,246
Rating: 4.9493394 out of 5
Keywords: MATLAB, Functions, Functions in MATLAB, User-Defined Functions, User Defined Functions, User Defined Functions MATLAB, User-Defined Functions MATLAB, Tutorial, Lesson, Example, Example Functions MATLAB, Lesson Functions MATLAB, Tutorial Functions MATLAB, MATLAB Functions, Function Arguments, Programming, Programming Concepts, Coding, Coding MATLAB
Id: aiKDMOytuVU
Channel Id: undefined
Length: 12min 6sec (726 seconds)
Published: Fri Oct 19 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.