How Does feval Work in MATLAB for Function Inputs?

  • Context: MATLAB 
  • Thread starter Thread starter shimo1989
  • Start date Start date
  • Tags Tags
    Matlab Work
Click For Summary

Discussion Overview

The discussion revolves around the use of the feval function in MATLAB, particularly in the context of passing functions as inputs to another function, specifically for implementing the bisection method. Participants explore how to properly utilize function handles and the syntax required for feval.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant expresses confusion about how to pass a function to their bisection function using feval, mentioning attempts with strings and function names.
  • Another participant suggests using @compute as the argument to indicate a function handle.
  • A participant clarifies that feval requires a function handle, recommending passing @function_name to the bisection function.
  • It is proposed that the bisection function can also accept a string representing the function name, which can be converted to a function handle using str2func.
  • A later reply indicates that passing the function name as a string to the bisection function works, though the participant remains unsure why it is effective.

Areas of Agreement / Disagreement

Participants generally agree on the necessity of using function handles with feval, but there is some uncertainty regarding the effectiveness of passing function names as strings and how that interacts with function handles.

Contextual Notes

Participants do not fully resolve the implications of using strings versus function handles, nor do they clarify the underlying mechanics of how these approaches work in MATLAB.

Who May Find This Useful

Individuals new to MATLAB, particularly those working on assignments involving function inputs and numerical methods like bisection, may find this discussion helpful.

shimo1989
Messages
3
Reaction score
0
I'm completely new at MATLAB, having just started using it about a week ago. In an assignment of mine, I have to make a function that does bisection. So I pass a function, along with a set of bounds, to the function I wrote.

The thing is, I can't figure out how to make my function accept the function I'm trying to input into it. I was told I could use

feval ( function-name , x-value )

to find the value of a function at a particular x value, but I could not, for the life of me, figure out how I can make feval recognize functions. I tried passing strings, names of other simple functions I made, and just about everything I could think of. Nothing works.

Then I tried looking online, and websites and MATLAB's built-in help confused me even more by telling me stuff about "function handles" and "@" signs.

So could someone please explain to me, in the simplest language possible, how feval works? I would be very, very grateful.
 
Physics news on Phys.org
If the function you want to call is 'compute' you'd enter '@compute' as the argument
 
But I have something like this

function returnvalue = bisectionfunc ( function_name , upper_bound, lower_bound )

blah blah blah

then

feval ( function_name , value_of_variable );

What do I pass into bisectionfunc as "function_name"? I'd like that to be the function on which I'm performing the bisection method.
 
The simple answer: Pass @function_name to bisectionfunc. feval wants a function handle, not a function name.
Code:
function bisectionfunc(fhandle,upper_bound,lower_bound)
  ...
  feval (fhandle, value);
  ...

That said, you could make your function take the name of a function as an argument as well as a function handle:
Code:
function bisectionfunc(fhandle,upper_bound,lower_bound)
  if ischar(fhandle)
    fhandle = str2func(fhandle);
  end
  ...
  feval (fhandle, value);
  ...
 
Ahh, I think I got it. I passed the name of the mathematical function as a string to the function I wrote, and somehow it works.

Still not sure why it works, but thanks everyone for your help.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K