1

I am looking to use the fminsearch function for minimization. According to the documentation, fminsearch requires a function handle and initial parameter estimate.

However, I have been struggling to create a function handle that accepts a variable number of inputs. This is an example of how I would like my code to behave:

for i = 1:M
    fhandle = @(x)(x(i) + @fhandle)
end

In this example, the final fhandle is the sum of all x. Is there any way to implement this and optimize all x values such that fhandle is minimized?

1 Answer 1

3

Function handles accept varargin. So your exemple can be rewriten to accept a variable number of inputs:

fhandle = @(varargin) sum([varargin{:}])

and then

>> fhandle(1)
ans =
     1

>> fhandle(1,2)
ans =
     3

>> fhandle(1,2,3)
ans =
     6

Best,

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.