How to Find the Derivative of a Function Handle in MATLAB
We will look at different ways to find the derivative of function handles in MATLAB.
We will use different example codes and related outputs to clear your concepts and give you a complete insight into methods to find the derivative of function handles in MATLAB. Please note that a function handle is a MATLAB data type that holds information about a function.
Indirectly calling a function allows you to execute the function from any point. Function handles are commonly used for the following purposes:
- Passing one function to another, often we call them function’s functions. Passing a function to an integration and optimization function, such as
integral
andfzero
, is an application. - Callback functions must be specified; for example, a callback that responds to a UI event or interacts with data acquisition hardware.
- Creating handles for functions specified inline rather than in a software file; anonymous functions.
- Local functions are called from outside the main function.
Let us understand the methods to take derivatives of function handles in MATLAB.
Use the diff
Function to Find the Derivative of Function Handles in MATLAB
Before looking into the methods of calculating the derivative of the function handle, understand how we can create it in MATLAB. Suppose we have the following code line:
Isa(i,'function handle')
It can be used to define a variable, i
, as a function handle.
To make a handle for a function, use the @
symbol before the function name. For instance, if we define a function called my_defining_function
, to make a handle labeled f
, we write the following:
f = @my_defining_function
MATLAB has no notion of whatever the parameters of a function handle symbolically signify. In the first place, we should make it with syms
.
Let us understand this concept by looking at the following example.
Code:
syms y
func = @(y) y^3 + 5;
diff(func,y)
Output:
ans =
3*y^2
Let’s look at some other examples.
We first distinguish a symbolic matrix function from its matrix argument and then determine the derivative of the function W(CX)=AXsin(BX*CX)
, where A
is a one-by-three matrix, B
is a three-by-two matrix, and X
is a two-by-one matrix. Generate symbolic matrix parameters A
, B
, and X
, and a symbolic matrix function W(CX)
.
Code:
syms AX [1 3] matrix
syms BX [3 2] matrix
syms CX [2 1] matrix
syms W(X) [1 1] matrix keepargs
W(CX) = AX*sin(BX*CX)
Output:
W(CX) =
AX*sin(BX*CX)
Now, taking the derivative:
Code:
Dt = diff(W,CX)
Output:
Dt(CX) =
AX*(cos(BX*CX) .* BX)
Looking at another example for further practice:
Code:
syms F(Y)
F(Y) = sin (Y ^ 4);
dF = diff (F, Y)
Output:
dF(Y) =
4*Y^3*cos(Y^4)
Mehak is an electrical engineer, a technical content writer, a team collaborator and a digital marketing enthusiast. She loves sketching and playing table tennis. Nature is what attracts her the most.
LinkedIn