Introduction to Feval in Matlab
MATLAB is a programming environment that is interactive and is used in scientific computing. It is extensively used in a lot of technical fields where problem-solving, data analysis, algorithm development, and experimentation is required. Software which are discipline-specific are 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. Feval function is used to make the code easy to understand or read, as we use parentheses in it for both function invocation and indexing.
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, additional performance, and offers source file controlling.
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 name of a function, we cannot use path information.
Examples of feval Matlab
Let us now understand how 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 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 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 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
In this example, we will combine two input strings using ‘strcat’ function. 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:
As we can see, by passing ‘strcat’ as input argument to our ‘feval function’, we are able to concatenate the 2 input strings passed as 2nd and 3rd arguments.
Conclusion
‘feval function’ can be used in MATLAB to pass both the function and its arguments inside one parenthesis. Please keep in mind that in its latest versions, MATLAB recommends function handle as better option than using feval.
Recommended Articles
This is a guide to feval Matlab. Here we discuss the introduction, syntax, Description, examples with code implementation respectively. You may also have a look at the following articles to learn more –
3 Online Courses | 1 Hands-on Project | 8+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses