How does feval work in MATLAB?

  • MATLAB
  • Thread starter shimo1989
  • Start date
  • Tags
    Matlab Work
In summary, you can use feval to find the value of a function at a particular x value. You can do this by passing the name of the function as well as a function handle.
  • #1
shimo1989
3
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
  • #2
If the function you want to call is 'compute' you'd enter '@compute' as the argument
 
  • #3
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.
 
  • #4
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);
  ...
 
  • #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.
 

1. How do I use feval in MATLAB?

Feval is used to evaluate a function in MATLAB. To use it, you need to provide the name of the function as a string and any input arguments the function requires.

2. What is the syntax for feval in MATLAB?

The syntax for feval in MATLAB is feval(function_name, input_arguments). For example, if you want to evaluate a function called "myFunc" with two input arguments, the syntax would be feval('myFunc', arg1, arg2).

3. Can I use feval to access built-in functions in MATLAB?

Yes, you can use feval to access built-in functions in MATLAB. However, you do not need to use feval to access these functions as they can be called directly without using the feval function.

4. How does feval handle variable number of input arguments?

Feval can handle a variable number of input arguments by using the varargin keyword. This allows you to pass in any number of input arguments to the function being evaluated.

5. Can I use feval to evaluate multiple functions at once in MATLAB?

Yes, you can use feval to evaluate multiple functions at once in MATLAB by using a cell array. The function names can be stored in a cell array and then passed into the feval function along with the input arguments.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
829
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
999
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
Back
Top