22

Is there an idiomatic way to bind variables in a MATLAB function? It seems like it would be fairly common to create a function, bind a few arguments, then pass the new function to an optimizer of some sort (in my case, a Newton solver). It doesn't look like the variable scoping rules permit a solution with nested or inline functions. Should I simply create a class? It doesn't seem like MATLAB has first-class function objects, is this correct? My search kung-fu is coming up short. Thanks!

As an example, suppose I want to find the roots of f(c,x)=x^3+cx^2+2x+3 for various values of the parameter c. I have a Newton's method solver which takes a function of one variable, not two. So I loop over various values of c, then pass the bound function to the solver.

for c=1:10
  g=f(c); % somehow bind value of c
  seed=1.1; % my guess for the root of the equation
  root=newton(g,seed); % compute the actual root
end
2
  • 1
    Could you provide an example of what you're talking about? What is it beyond f = @(x,y)(x + y)? Commented Feb 6, 2012 at 0:05
  • 1
    Do you mean a function returning a function? e.g. g=@(base,x)( @(x) mod(x,base) ); f=g(3); and now f(x) is just mod(x,3)? Commented Feb 6, 2012 at 0:08

2 Answers 2

26

You can do it like this:

f = @(c,x)( @(x)(x^3+c*x^2+2*x+3) );

for c=1:10
    g=f(c); % g is @(x)(x^3+c*x^2+2*x+3) for that c
    ....
end

The key is the first line: it's a function that returns a function.

I.e., it returns @(x)(x^3+c*x^2+2*x+3), with the value of c bound in.

8
  • 1
    A function defined with the @(args)( return val ) is called an anonymous function, and f (and g) are also called "function handles". See here. Commented Feb 6, 2012 at 0:19
  • 2
    Impressive, I just verified this in Octave. The second argument to f turns out to be a dummy value, then. Commented Feb 6, 2012 at 0:20
  • 2
    Yep, it's just a placeholder. I used to use this quite often for functions of x with associated parameters. Commented Feb 6, 2012 at 0:28
  • 1
    In C++, if a class has overloaded the ()-operator, then you can use a reference to an instance of that class just like you would a function. en.wikipedia.org/wiki/Function_object#In_C_and_C.2B.2B Judging by the following link, it looks like this overloads the subscript operator in MATLAB. mathworks.com/help/techdoc/matlab_oop/br02znk-1.html
    – dls
    Commented Feb 6, 2012 at 5:08
  • 3
    The x in @(c,x) is superfluous. I think f should better be written as: f = @(c) @(x) (x^3...)
    – knedlsepp
    Commented Apr 7, 2015 at 14:01
0

I am pretty sure that a nested function can be used with fminsearch. I don't know specifically about Newtons method, but my guess is that there is no problem.

1
  • Unlike inline functions (which seem rather pointless given the existence of nested and anonymous functions), nested functions would work as any variable in the nesting code block is in scope. The only down side is you have to explicitly give these variables a name to access them. This is ok if you're going to use them for something else, but seems redundant if you're going to define and initialize it if it's only going to be referenced within the nested function.
    – dls
    Commented Feb 6, 2012 at 5:13

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.