Function Handles

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

The last MATLAB unique data type that we're going to discuss

is function handles, and we'll see


that this will be very important when
we're looking at specific toolboxes
for scientific computing.
It also can simplify your code significantly.
This allows callability to a MATLAB functions
stored within a variable itself.
So you're invoking the function in a sense indirectly,
and it allows the function call outside of the normal scope
of a traditional function file.
This can capture data for later use
and enable passing functions as arguments.
We'll see that many of the toolboxes for optimization
require function handles to be passed
in for your objective function and also your constraint
functions.
This is similar to solving nonlinear system of equations,
passing in ODEs, and numerical integration.
We'll be looking at an example with the trapezoidal rule.
One thing to note, and limitation about function
handles, is they must be scalars,
so it cannot be an array accessed with parentheses.
Let's look at the simple example for the trapezoidal rule.
If you'd like to follow along, I have
included the code traprule.m, which
has a function that implements this integration here,
numerical integration.
And also, this part is computed in session
1.m. So please play along with the code.
One important thing about functions
is we see that we would have to write out
this function at every single data point within our code.
But function handles allow us to pass
in an arbitrary f function, so it
looks very clean and almost like the math syntax, in a sense.
So if we look at what this trapezoidal rule is doing,
we're passing in our function handle
f, we're passing in the lower limit and the upper limit
of the integrals, and also the number of elements
that we want to sum over.
The larger this is, the more accurate
our integration will be.
And then we can simply write this integral
in very similar math form here.
So we see that this is also written
in a vectorized fashion, as what we talked about in the code
performance section.
Sum is a vectorize operation, which
will sum over all the elements of this array
and all the elements of this array here.
So this represents our f of x of i minus 1/2.
We're going from 1 to n minus 1.
Remember what the colon operator does, and also end keyword
will go to the last element an array.
And here, this is our f of x i plus 1/2.
Here we're doing a simple example
where we want to integrate our function, e times x squared.
This is a simple integral you can do by hand,
but it just illustrates this function.
So here, our function handle is denoted by the ampersand at x.
So x is going to be our independent variable.
So all we need to do is pass f into this trapezoidal rule
function, and it computes its approximate integral,
and you see the exact value is given here.
So passing this f will be the exact same way
that we do it in the ODE and optimization toolbox.
And you'll see that this would have been much messier
if within this code we had to write a times x dot squared
evaluated at these points.
And if you have a more complicated function,
it will become even messier and more difficult to read code.
So function handles are a very nice way to simplify your code.

You might also like