Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

How does feval work in MATLAB?

  1. Jan 30, 2010 #1
    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.
  2. jcsd
  3. Jan 30, 2010 #2


    User Avatar
    Gold Member

    If the function you want to call is 'compute' you'd enter '@compute' as the argument
  4. Jan 30, 2010 #3
    But I have something like this

    function returnvalue = bisectionfunc ( function_name , upper_bound, lower_bound )

    blah blah blah


    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.
  5. Jan 30, 2010 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    The simple answer: Pass @function_name to bisectionfunc. feval wants a function handle, not a function name.
    Code (Text):
    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 (Text):
    function bisectionfunc(fhandle,upper_bound,lower_bound)
      if ischar(fhandle)
        fhandle = str2func(fhandle);
      feval (fhandle, value);
  6. Jan 30, 2010 #5
    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook