MATLAB Tutorial Lesson #04b: Multiple Input Output User Defined Functions

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is Professor Rudy and this video is about how to create user defined functions in MATLAB that have multiple inputs multiple outputs or both first I'd like to start with the case of multiple inputs so to define a function if you remember we want to start a new file when you start that with the word function then the first thing that you want to put here is what your output is going to be for this case I'm going to do the example of how to calculate the acceleration in the normal direction so I'll call that a sub n that's going to be my output that's what I want to calculate so I'm just calculating one thing in this function and then that output equals I need to give the function a name so I'll call my function normal acceleration and what I need to calculate the normal acceleration is the velocity and the radius of curvature so if I put these in here these are the inputs to my function so if I can give this function the velocity in the radius of curvature I can get a calculation of the normal acceleration what comes after the function is common so in here you can put name date whatever useful information about this file you want to include it's always good to say what the function does this function calculates the normal acceleration then it's a really good idea when you're defining your own functions to put a description of what the inputs and outputs are so the inputs here are velocity which is going to be given in some units so if you know what units you're going to be working in here it's great to include that here so if we give velocities in meters per second and radius of curvature meters that will give me an output of a sub n which is the normal acceleration in meters per second squared then we want to include our calculation here and whatever we put inside this function is what that's going to do when this function is called so for this case it's relatively simple we have that the normal acceleration is the velocity squared divided by radius of curvature and that's all we need so this is similar to what we've talked about before on another video with y equals some function of X you can have that be a function of as many things as you need for your calculation so in this case we have those two values now when you go to save your function you always want to save it with the same name here as you have here that way MATLAB will know where to find that function so it can find this file to know how to run that code so once we save that then we have this function that we can call if you call this from the command window we could call it from a script so from the command window we can say normal acceleration and then if we had a velocity of 10 meters per second and a radius of curvature of 2 meters we can run that and then we'll get an answer and that's telling us that we'll have a normal acceleration of 50 meters per second squared I'd also like to point out that how we have these comments here in our function you don't even need to have the function file open to see this information what this is is this is what the help command will do if I call help of normal acceleration what it's going to do is it's going to pull up those comments and display them on the command window so even if I didn't have this function file open I can still see that and get an idea of what this function is going to do and how to how to call it what arguments it needs so this is an example of using multiple inputs but a single output so next let's do a new example and here I want to talk about a case of multiple outputs so you still define things in the same way I do function but because I want multiple outputs instead of just having one variable here I wanted use a vector and I'm going to place this vector in square brackets and I'm going to define each of the outputs that I'm interested in for this example what I want to do is calculate some statistics for some given vector so the ones that I'm interested in are the mean median Max and min let's say and these could be whatever values you're interested in now I have these as Y underscore mean median Max min because these names are functions we don't want to use variables that overlap with function names so I'm putting that in there and I'm using Y just because that's what I'm going to call my input so I'll call this function calc stats and my input there will be Y so Y is some vector and then I'm going to calculate some statistics for that vector we can do comments to describe what's going on so here this function calculates statistics for a given vector my inputs are just going to be Y which is some vector and my outputs are so that why mean is the mean of the input vector Y median is the median of the input vector y Max is the max of the input vector Y min is the min of the input vector okay so remember we put these comments in there that's something we'll be able to see with the help command so that's really useful information the more we put in there the better I would for more complicated functions I would expect to see even more than this in there telling us exactly what's going on with that function but for here this is probably good enough now inside this function we need to define each of those outputs if you notice here we're getting all these MATLAB warnings here this is saying that function return value is not set we have not defined Y mean or median Max or min we also even have a warning here that's saying you're not using Y so if a function is not using an input it shouldn't be there and if an output is not defined you're not going to get what you want so we need to do that so we can do this and these are pretty simple Y mean will be the mean of the input vector and so on for median Max and min so if we do this save our function remember I want to keep that same name so if I call my function calc stats I need to save it as calc stats or things will not work so if I save that now what I can do is I can calculate the statistics for some vector now I don't need to define this vector with the variable Y Y is just what it's going to be called within the function so I could even call some variable X which is from 1 to 10 and then I can do calc stats of X now what happened here is when I called that and didn't define any of my outputs it's just giving me one number and we know we wanted multiple outputs so what I need to do is actually explicitly spell these out of what I want each output variable to be so here I'm using X's instead of Y's because I'm talking about some variable X and now we have up and I have a typo all right try that again okay so we can see here then we calculated the mean of five point five median was also five point five max was ten min was one and you can do this for any input factor you could create a let's use a different variable now let's say noise and create some random noise and let's do let's do a million points ten to the six million points so if we just look quickly at what this looks like we've got this noisy signal here so I don't know some statistics about this signal so say noise mean always median always max noise min is calcs that's noise I do that I get my values here so I had a mean value of this median value here and then my Max and minimum values and this is a way that you could use a function to get multiple pieces of information even with only one input vector so for my third example this will be multiple inputs and multiple outputs so you can have both and for this example I'm going to use a dynamics problem a simple dynamics kinematics problem so here's the problem we're throwing a ball from a tower that has some height H and we're throwing it at some initial speed V naught and we want to calculate the speed when it hits the ground and how long it took to get there so when I'm going to solve this problem using a function so I've already defined my equations using kinematics so I started with this equation here which relates the velocities and position through a constant acceleration my change in position is just going to be equal to the height of the tower my constant acceleration is equal to the acceleration due to gravity and then if I solve this for velocity I can get this relationship so notice we have V as a function of initial velocity and height and we also have this known constant G for acceleration due to gravity then to find the time we can use this equation which relates the velocities and the time through that constant acceleration and we can solve this where now we have time in terms of the velocity which we've calculated the initial velocity and acceleration due to gravity and I want to program these calculations into a function so I can solve them for different cases of height and initial speed so function let's call the outputs here we have the velocity V and the time T and the name of this function we want to have that somehow relate to what we're doing here so maybe I'll call this baseball tower that's probably not the best function name but that's what I'm coming up with at the top of my head and what we need is inputs then are the values defined as the the given values in the problem so we have an initial speed V naught and then the height H so notice we define our outputs in a vector here so we are the things we want to get and here the inputs these are the things that we're going to give in the function so I'll quickly put some comments in here okay you could also write stuff about your inputs in your output so I'm going to just cruise past that for the interest of time here so then what we want to do inside this function is define the velocity so V equals square root of V naught squared minus two times G times H so V naught and H are given in the problem but G is actually not so we're going to need to define what that G value is so we could put that here and in this case let's assume we're in feet per second squared so because we have this G and we have this in specific units it actually is going to be very important so I will actually do it go up here and define our inputs and outputs so the input V naught is initial velocity meters per second H is tower height and and I'm already doing it it has to be in feet per second and feet for these calculations to work out correctly so then if I do that I'll get outputs of the final velocity will also be in feet per second and then the time of travel will be in seconds so with these given values as salt as long as we have the right units then we have this value of G and we can program this in here actually realize a phone here sorry about that that should be a plus so we calculate that velocity and then we can also calculate the time once we have that velocity B we can use that in our second calculation for time so here calculate the final velocity and here calculate the time of travel you save this we want to save that as baseball tower that's good so then just like with the others we need to define this so if we want to calculate the velocity and the time we can call the baseball tower function and then give it arguments for V naught and H so if we have an initial speed of eighteen feet per second height of 50 feet you can solve that and then we'll get the values for velocity in feet per second and the time of travel in seconds so that's some examples of defining user-defined functions for multiple inputs multiple outputs or both thank you
Info
Channel: Matthew Rhudy
Views: 81,760
Rating: 4.769784 out of 5
Keywords: MATLAB (Programming Language), Tutorial (Media Genre), Engineering (Industry)
Id: d9bWSuvWudY
Channel Id: undefined
Length: 16min 16sec (976 seconds)
Published: Mon Aug 03 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.