Video: Function m-files (7.13)
Transcript
So, by now you know all about script m-files - how to create and how to execute them. There are another special type of m-files called function m-files. Function m-files can not be executed as such, but you can call them from the Command window or from a script m-file or from another function m-file. And you can do a lot of useful and amazing things.
As you know there are several built in functions in MATLAB. In fact, almost every command that you have been using in MATLAB is stored as a function m-file in MATLAB software. You have freedom to create and store your own functions in MATLAB as a function m-file. However, it must have a specific structure and should follow certain rules.
Let's open a function m-file template. Go to this New and click over this function. This opens the new function m-file template. Look at the structure of this template. The first word is always function. Then you should have output arguments, and then after the equal to sign, you should have the name of the function, which is Untitled5 at the moment, and then in the bracket you should have input arguments. And after that you can comment what ever you want - some description about the function - and then you can define your function. So this is the general structure of a function m-file.
Let us define a function so that it would make more sense. So - the first word has to be function - so no need to touch that. You can have output argument as y; you can have input argument as x; you can have more than one input and output - but this is the simplest thing to start with. And you can give some name to your function - say - dadada - well you could have any name but it should not be the same as any enabled function name or already existing function file name.
Ok - Next thing is to define the function. So output is y, you can say y is equal to what ever your function is sine of x, plus x cubed. Ok. And you can put a semi-colon so that you don't see unnecessary output in your Command Window. And that is it. Maybe you could put some comments about this function. You could say this is a test function. OK.
And the next thing is to save this function file. OK - remember always function file is to be saved as the same word here - dot m. So name of the function, dot m, so it is dadada.m. So, Let's save it. Save as - see MATLAb already know this is the function m file and it is already taking the dadada.m. OK. Save it. It is saved now.
OK- now, one thing remember that you can not run a function m-file. Try running it - see what happens. Click on run - see some errors there and actually you can see error using dadada - not enough input arguments. So you need to give some inputs. Ok, so let's give some input.
Say I want to calculate my function for x is equal to 3. It is calculated. You can calulate for anything - x equal to 7. It gives you that number.
Now - will this function work for a array input? So for example if I want to calculate this function dadada for an array - say 3, 4, 5, 6. Would it work? No it doesn't.
But error itself is suggesting to you a hint. you can use a dot somewhere. OK let me go to my m-file, and I want to make it to work for any array. So what I need, I need a dot here. And save it. And then go to Command Window again and see if it works - yes it does. It has calculated all those 4 numbers which correspond to x equal to 3, 4, 5 and 6.
In fact now your function will work for any array. So for example you can calculate this array say 1 to 100. So all the numbers 1, 2, 3, 4 up to 100. It works for all of them. See all those 100 calculations are done in just one hit.
OK. Let me clear the screen so we can see other things.
OK. Once your function is defined, you can operate other functions over it. For example, you can use fplot fplot, to plot your function over any given interval. So fplot and my function is dadada and I want to plot it between minus 10 to plus 10.; close the bracket. And it is plotted. So it is plotted from minus 10 to plus 10. If you want to change colour you can do further things, you can put red here, r. It will plot it in red.