· In our work in the past few
weeks, we have used several mathematical functions without much discussion. As you
might expect, MATLAB provides an extensive library of built-in functions
covering most of the common mathematical operations including exponential and
logarithm functions, trigonometric and hyperbolic functions, and some common
rounding functions. The functions have
a common, and by now familiar, syntax:
output =
function_name(input);
For example y=sin(x);
· Typically we refer to x as
the input or argument of the function that has a
function name of sin. The output or returned variable, which would be sin(x), is said to be returned by the function.
· These
standard or built-in functions in MATLAB are fairly powerful and often operate
on both scalar and array arguments. In addition, it is possible to use
functions in the arguments of other functions (a technique which is known as
function composition) e.g. y=sin(sqrt(x)).
· If there is some operation
you use frequently that is not already a predefined function in MATLAB, you can
create a user-defined function. These function files are simply m-files with
a specific structure.
·
Using
the MATLAB editor we are able to construct our own function files that can be
used repeatedly by other scripts files or other functions. This process is
analogous to the use of subroutines in other languages such as C and Fortran.
Essentially, this means that we can construct our own library of functions
relevant to our own interests and applications. The main benefit or consequence
is that complicated programming problems can be decomposed into smaller tasks
and programs can then be developed in a modularized and structured manner.
·
To
do this we define and code function files – a special type of m-file, that have
a well specified structure or syntax. The first
line on the function file must contain a function definition line that
defines the function name and specifies a list of input and output variables:
function [output
variables]=function_name(input variables);
q
output variables is a comma separated list of variables calculated
within the function file and enclosed within square brackets in the function
definition. These are the values that the function will return to the MATLAB
workspace.
q
input variables is a comma separated list of variables that need to
be passed to the function when you use it. They are akin to the
argument of the trigonometric functions we looked at earlier.
q
function_name is the name that you have chosen for the function
and MUST be the same as the filename that you use to save the m-file (but it
does not include the .m extension)
q
Any
other variables defined inside the function but not in the output variable list
are local variables and are not returned to or shown in your workspace. You may use variable names inside a function
even if those variables are used in a different way in the calling function.
·
The
word function in the function definition line
must be in lower case.
·
Comments
can be placed anywhere within a function definition but those immediately
following the function statement will be printed when you type help ‘function name’ at the command prompt.
·
There are many instances where two variables are
related by a square-law relationship, particularly in terms of energy and power
calculations. Since these must be calculated on a frequent basis it is useful
to define a special function for this purpose. Some illustrative relationships
are:
Power
dissipated in a resistor: ![]()
Kinetic
energy calculations: ![]()
·
Let’s now write a function file whose sole
purpose is to compute the square law relationship for some input…
· Open a new m-file and copy-and-paste
the commands below into that m-file.
Name the m-file the same name as the function name on the first line
(squarelaw.m). Add you name and the
date in the comment lines. Note that
when copying-and-pasting you may get blank lines between the comment
lines. You MUST
delete these blank lines in your m-file so that the first group of comment
lines are not interrupted by spaces.
function
[out]=squarelaw(in,param)
%
% [out]=squarelaw(in,param)
%
% Computes square-law relationship
% out=(in^2)*param
%
% Author: …
% date:
…
out=(in^2)*param;
·
Type
help squarelaw in the command window and you
should see the comment lines to remind you what this function does and what its
input and output arguments are.
·
Test
it out… Run the function as follows at
the command prompt:
>> out=squarelaw(10,2)
·
Now,
modify this function so that it will allow both scalar
inputs and vector (array) inputs and outputs and
test it. Hint: make mathematical operations array
operations rather than scalar operations.
Test it like so at the command prompt…
>> v=linspace(0,10,100);
>> R=1000;
>> p=squarelaw(v,1/R);
>> plot(v,p)
>> xlabel('Voltage (V) ')
>> ylabel('Power (W) ');
>> title('Power Dissapation for a 1000 Ohm
Resistor');
·
Note that all these commands above can be put in a script
file (m-file). So m-files can call
functions and one function can call another.
Thus, it is helpful to build a library of commonly used functions. You write an operation once as a
well-written function, then you can use it anytime you need to perform that
operation without having to retype it.
This is called modular programming.
This document was originally created by Dr. Malcolm
Daniels of the Department of Electrical and Computer Engineering and modified
by Dr. Russell Hardie.