Inline Functions in MATLAB
Inline functions are those functions that are defined in one line, also called one-liner for the same reason. In MATLAB there are two types of inline functions, inbuilt and user-defined. Let’s take a look at both of them.
InBuilt inline Functions:
MATLAB has many mathematical functions built-in which are inline such as log(), sqrt(), sin(), etc. It also offers more complex mathematical functions as inline functions such as the beta and gamma functions, etc.
Example 1:
Output:
User Defined Inline Functions:
MATLAB provides the option to define inline functions in the script using the inline keyword. The syntax for the same is
function_name = inline(‘expression’, ‘variable’)
The expression is the function’s expression and the variable is the independent variable of the function.
Example 2:
Matlab
func = inline( ' x^3 + x^2 + x' , 'x' )
|
Output: We can call this function with various parameters:
Vectorized inline functions:
Inline functions can be vectorized using the vectorize keyword. The syntax is
function_name = inline(vectorize(‘expression’), ‘variable’)
Example 3:
Matlab
func = inline( ' x^3 + x^2 + x' , 'x' )
func(-1,3)
func(0.5:1.5)
|
Output:
Conclusion:
This article discussed how to use built-in inline functions of MATLAB and how to define inline functions in a MATLAB script. We also discuss all the various options that could be used with user-defined inline functions and how to call those functions with various parameters.
Last Updated :
28 Sep, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...