In this video, we will show you how to use function
handles in MATLAB for engineering applications.
A function handle is a data type that describes a function.
This could be a named function or an anonymous function.
This is an important concept to know in MATLAB if you're
trying to use a function that only accepts
another function as an input.
First, a named function handle represents a function that
is stored in existing program files including built in MATLAB
functions and custom functions you write yourself.
The other type of function handle
is an anonymous function.
This represents a function that is stored in the same file
that you're calling the function.
This is generally much less sophisticated,
can execute only a single statement,
and only return a single output.
First, let's write a basic function, custom sum.
The sum function needs to take an input array
x with any number of elements and output the sum
of the elements.
First, open up a new function.
This function will only have a single input x
and a single output sum of x.
Next, let's identify the number of elements in the array
by using size.
The two outputs of the size function
are number of rows and number of columns.
Let's then initialize the variable SUM_var
that will store the sum of the elements.
Then use a for loop to add every element in the array one
at a time and stored in SUM_var Lastly,
let's set our output variable as SUM_var.
Remember to save the function into the same folder
as our function handle script.
Let's go back to our main script and see if our function works.
The resulting sum of x is 10.
Let's take this function one step further
and use it to write our own averaging function.
This time we will write this as an anonymous function.
The average function needs to take an input array x
and additionally the number of elements in the input array
and output the average of the elements.
First, let's use the size function again
to determine the number of elements.
Next, let's create an anonymous function handle customMean
and initialize it with the at symbol followed
by the variables that represent the inputs.
This is followed by the statement
sum of x divided by n, which outputs the average.
Let's use our function to calculate the average of x.
The mean is 2.5.
Notice the mean becomes a function
handle in the local workspace, and it is not
stored as a separate file like our custom function sum.
Lastly, let's show an example of how
to use an anonymous function handle as an input
to the MATLAB built-in integral function.
We're going to create our function handle
inside the same script we're calling the integral function.
Let's say that you're given the following equation,
and we want to find the integral of this from 0 to infinity.
This equation would be tedious to solve by hand,
so I think you will appreciate how much easier it
is to solve in MATLAB.
First, we name our function handle.
The at symbol initializes the function
followed by the input variables of the function.
x is our independent variable.
So we will integrate over x, which
is why our input for our anonymous function
needs to be x, which is then followed by the statement.
After calling the integral function,
the first argument is the function
handle we just created, and the output
is our function integrated from 0 to infinity.
Our answer is 1.9475.
Now, let's solve the same problem using a named function.
Our input will be x and our output will be y.
Let's name our function fun2.
The output, y, will be equal to the same equation previously
defined.
We can save the function in our local file.
We can now create our function handle.
Let's call it FUN with capital letters,
and set it equal to our function fun2.
Our function is represented with an at symbol followed
with the name of our function.
Now we're ready to use the integral function.
The first argument is now the named function
handle FUN with capital letters.
We should get the same answer as before.
In conclusion, we discuss how to write
two types of functions, named functions
and anonymous functions.
We also discuss how to use the functions
as an input to other functions.
If you want to learn more about function handles or any
of the functions we use in this video,
please see the links in the description.