10.10 Introduction to Chemical Engineering
NMM Chapter 3: Programming in MATLAB
Defining Functions
When you use “sin” or “log” or “exp” in Matlab you
are using “function m-files”. They are different from “script m-files”
primarily because they have inputs and outputs. To specify which variables
in the m-file are the inputs, and which are the outputs, the first line
of the m-file should be in this form:
function output=function_name(input)
For example, an m-file that begins with the line:
function L=vec_length(V)
takes a column vector V as its input, and returns
its length L as its output. Here is the complete file vec_length.m :
function L=vec_length(V)
% returns the length
of column vector V
L=sqrt(V' * V); %
square root of the dot product of V with itself
You would invoke on the command line this way:
>> W=[1; 2; -3]
>>Length_of_W=vec_length(W)
Several more examples are given in NMM 3.2, and
on the 10.10 Web page. We recommend you make the name of the function given
on the first line match the file name of the m-file, i.e. function vec_length
is in file vec_length.m, not in sillyname.m
We also recommend you put only one function in each
m-file (though under some circumstances it is allowed to group a set of
functions together into a single m-file.) If you store all your m-files
in the same directory, any function can call any other function. If you
get a message saying that Matlab cannot find your m-file, on the command
line type
>> pwd
to make sure Matlab is looking in the correct folder,
and then
>> dir
or
>> ls
to make sure that the m-file is really in that folder.
You may have accidentally saved it in the wrong folder.
Functions of Multiple Variables
Functions can have any number of inputs and outputs.
Here is an example with three inputs and three outputs:
function [x,y,z]=Spherical_to_Cartesian(r,theta,phi)
% converts from (r,theta,phi)
to (x,y,z) assuming theta and phi in radians.
%x,y,z
will have the same units as r
% last revised by W.H.
Green, Aug. 22, 2002
z=r*cos(theta);
x=r*sin(theta)*cos(phi);
y=r*sin(theta)*sin(phi);
% Sample input/output:
%>>
r=2; theta=(pi/2); phi=pi;
%>>
[x,y,z]=Spherical_to_Cartesian(r,theta,phi);
%>>
x
%
%x
=
%
%-2
% >> y
%
%y
=
%
%2.4493e-016
%
% >> z
%
%z
=
%
%1.2246e-016
(Note that in exact math, y=z=0, but roundoff errors
cause them to take these very small nonzero values. We discuss this more
below.) If you want, you can lump variables together into arrays, e.g.
function V=Spherical_to_Cartesian_Vector(r,theta,phi)
% converts from (r,theta,phi)
to V=[x;y;z] assuming theta and phi in radians.
%Components
of V will have the same units as r
% last revised by W.H.
Green, Aug. 22, 2002
z=r*cos(theta);
x=r*sin(theta)*cos(phi);
y=r*sin(theta)*sin(phi);
V=[x;y;z];
% Sample input/output:
%>>r=2;
theta=(pi/2); phi=pi;
%>>Cart_vec=Spherical_to_Cartesian_Vector(r,theta,phi);
%>>Cart_vec'
%
%ans
=
%
%-2.00000.00000.0000
Note that in this case the variables x,y,z only exist inside the function,
they would not interfere with any other variables named x,y,z used on the
command line or inside other functions. Chapter 3.3 discusses how
to make your functions get input from the keyboard or from a file, and
how to save output to a file; you can skip 3.3 until you need to do this.
Functions can call other functions. This allows
you to break tasks up into pieces, and to write small functions that solve
each piece separately, rather than trying to do the whole thing at once
in a giant m-file. This is called modularization, and we strongly
recommend you use it!
(Functions can even call themselves, this is called
recursion,
a too-tricky method we recommend you avoid, as it often leads to bugs and
sometimes to slow execution times.)
Programming Style Requirements for 10.10
Each function should start with its name, then a
comment line giving the purpose of the function, its author and the date
when it was last revised, and if it is a homework assignment specify which
problem it is solving and give your email address.
Then give details about the required inputs and the outputs: what they
mean physically, and what units they should be in. Also, list the other
user-defined functions this function relies upon. (Do not list Matlab’s
built-in functions like cos or exp). This set of information is called
the “header”, and it is all the information that someone should need to
run your function correctly (i.e. if the function works, they shouldn’t
have to look at the rest of the m-file at all.) It is critically important
that this section be clear and complete!
To help yourself remember what you did (and to help the grader) you
should add a few additional comments inside the body of the function, particularly
at points that seem tricky or not-obvious to you as you write the program.
Finally, all functions should give a sample input and output as a comment
at the bottom. You can just cut and paste the appropriate section from
the Matlab command line or history into the function, and put % in front
of each line.
last modified: August 29, 2002