Updated July 3, 2023
Introduction to Feval in Matlab
MATLAB is a programming environment that is interactive and is used in scientific computing. It is extensively used in many technical fields where problem-solving, data analysis, algorithm development, and experimentation are required. The discipline-specific software is extensively written using MATLAB.
In this article, we will study how to use ‘feval’ function in MATLAB.
Before we start learning how ‘feval’ function works in MATLAB, let us understand why we need it.
Feval Function:
Feval function is used to evaluate the function output of a function by using the arguments passed inside a single parenthesis input. It accepts the function name passed as a “string” as its first argument. Please keep in mind that MATLAB suggests the use of a ‘function handle’ for the purpose of reference invocation. Function handles have the advantage of reliability and additional performance and offer source file control.
Let us now understand the syntax of ‘Feval function’ in MATLAB:
Syntax:
[a1, a2, ..., aN] = feval (function, inp1, inp2, ..., inp M)
Description:
1. feval takes a function as its first argument, which is passed as a string (in quotes)
2. The name of the function is usually defined by M-file
3. feval (function, inp1, inp2, …, inp M) will evaluate the value of ‘function’ at the input arguments.
4. Function parameter here must be the name of a function; we cannot use path information.
Examples of feval Matlab
Let us now understand what the code for ‘feval function’ looks like in MATLAB, with the help of various examples.
Example #1
In this example, we will find the average of elements of an array using ‘feval function’ We will need to follow the following 2 steps:
1. Create an array whose average we need
2. Pass the function ‘mean’ as a string inside the function ‘feval’
Code:
A = [1 4 6 7 0 2]
feval (‘mean’, A)
Input:
A = [1 4 6 7 0 2]
feval ('mean', A)
Output:
As we can see, by passing ‘mean’ as an input argument to our ‘feval function’, we are able to get the mean of our input array.
Example #2
In this example, we will find the standard deviation of elements of an array using ‘feval function’ We will need to follow the following 2 steps:
1. Create an array whose standard deviation we need
2. Pass the function ‘std’ as a string inside the function ‘feval’
Code:
A = [2 4 9 15 0 2]
feval (‘std’, A)
Input:
A = [2 4 9 15 0 2]
feval ('std', A)
Output:
As we can see, by passing ‘std’ as an input argument to our ‘feval function,’ we are able to get the standard deviation of our input array.
Example #3
In this example, we will find the value of the sine function at a particular point. We will need to follow the following 2 steps:
1. Pass the ‘sin’ as a string inside the function ‘feval’
2. Pass the point at which we need to get the value as 2nd argument
Code:
A = -pi/2
feval (‘sin’, A)
Input:
A = -pi/2
feval ('sin', A)
Output:
As we can see, by passing ‘sin’ as input argument to our ‘feval function,’ we are able to get its value at -pi/2.
Example #4
We will combine two input strings using the ‘strcat’ function in this example. We will need to follow the following 2 steps:
1. Pass the two input strings inside the function ‘feval’
2. Pass the function ‘strcat’ to concatenate the 2 strings
Code:
a = feval ('strcat', 'learn', ' feval')
Input:
a = feval ('strcat', 'learn', ' feval')
Output:
Conclusion
‘feval function’ can be used in MATLAB to pass both the function and its arguments inside one parenthesis. Please remember that in its latest versions, MATLAB recommends function handle as a better option than using feval.
Recommended Articles
This is a guide to feval Matlab. Here we discuss the introduction, syntax, Description, and examples with code implementation, respectively. You may also have a look at the following articles to learn more –