In this screencast I am going to show you the basics of working with M-files, anonymous
functions and user-defined function in matlab. First we are going to talk about script files
or also known as m-files in matlab. These are analogous to macros in excel and VBA.
You can create a file with a dot "m" extension using matlab editor. This is useful a lot
of times when you have commands that you are running over and over and you want to store
them somewhere. Then you can just run the m-file script by typing its name in. I am
going to go through an example here related to the Van der Waals equation which is shown
here. Let's say you work a lot with air. You have "a" and "b" which are just values in
this Van der Waals equation. We have "R" as the gas constant. We have temperature (T),
Volume (V) and number of moles (n). You want to calculate the pressure that amount of moles
at that volume and temperature exerts. Let's go into the matlab command window. I am going
to go up here on the upper left and create a new script. This brings up the matlab editor.
I type this in and you notice I have comments here which are denoted with a percentage sign.
I have the gas constant, "a" and "b". Now what I can do is save this. You have to make
sure that you directory is set to the location that you want your file to be saved in. I
am going to name this "setupVDWair". I can close the matlab editor and go back to the
command window. If write in setupVDWair it actually runs those commands and you notice
here on the right it has stored "Rgas", "a" and "b" which I defined in the m-file. Now
that I have this setup i can type in things like the volume, temperature and number of
moles. Then finally I can type in an equation for the pressure. Notice that "Rgas", "a"
and "b" came from our setupVDWair script. When I press enter it calculates a pressure
to be 1.1*10^5 Pa. That is a basic script file and I am going to refer back to this
example in a few moments. First I want to introduce function. There are a couple of
different types of functions. There are the built in functions to matlab and then there
are things called the anonymous functions and user-defined functions. An anonymous function
is one that you can define straight in the matlab command window. I am going to just
define a very simply anonymous function. I am going to call it charlie = @(x) x^2 plus
5 which the @ sign tells matlab that you are defining an anonymous function. When I press
enter it echos that and gives the anonymous function as a function only of "x" and the
function is x^2 +5. Then if I type in charlie(3) it is going to calculate 3^2 + 5 =14. It is
not going to be stored permanently but if you are working with a particular equation
a lot in one session in matlab then it is nice to declare or design these anonymous
function. When I clear it is actually going to clear the history of that charlie function.
We want to use matlab to create a user defined function. We have a spherical tank and we
want to know depending on the height in the tank what volume of liquid remains in that
spherical tank. I have an equation here for the volume as a function of height. The radius
is fixed and we want to create a function in matlab. The first step is to create a new
script here. I then type in the equation. I have volume =sphereTank. The name of my
function is sphereTank. It has two arguments; radius and depth. Where I have volume I could
have put anything there it is just the output of this function. Pi is defined in matlab
and you will notice I have these dot multiply. I am not necessarily doing item by item but
I want this function to be capable of doing item by item so that i could do the function
on a vector of the inputs. I have a semicolon here to suppress the output. I can save this
as a new file. I am saving this as sphere tank. The default when you save is actually
the name of the function. Now I am going to go back to matlab and type in sphereTank(5,1).
The radius is 5 and the height is 1 and this will tell us that we have 14.7 cubic meters
left in the tank when there is a height of 1 meter.