How to pass summation of function handles to fminsearch

8 views (last 30 days)
Albert on 16 Mar 2017
Answered: John BG on 20 Mar 2017
Hi, everyone
I am trying to pass a sum of function handles to fminsearch, however it always give me the error: Not enough input arguments. Error in @(i,x)sum(fun(2:3,x)) Error in fminsearch (line 200) fv(:,1) = funfcn(x,varargin{:});
Here is the simplified version of my code:
fun = @(x,i)100*(x(2) - x(1)^2)^2 + (i - x(1))^2;
sum_A=@(i,x) sum(fun(2:3,x)); // I have to do this summation in my real code
x0 = [-1.2,1];
x = fminsearch(sum_A,x0);
Why does it aways throw me an error that Not enough input arguments? In fact, sum_A just has 2 variables, what is problem here?
Any help is greatly appreciated! Thank you very much

John BG on 20 Mar 2017
ok,
it's all about making fminsearch work with the 2 input function as defined, right?
define the 2nd vector in advance the same way that you have defined x
s=[1 2 3];x=[3 4 5]
x0=[-1,2 1] % initial for fminsearch
this works
f = @(x,c) x(1).^2+c.*x(2).^2;
c = [1.5 2];
fminsearch(@(x) f(x,c(1)),[0.3;1])
=
1.0e-04 *
-0.244731948340174
0.315866965061825
.
and this works too
f = @(x,c) x(1).^2+c(1).*x(2).^2; % The parameterized function.
c = [1.5 2]; % The parameter.
fminsearch(@(x) f(x,c(1)),[0.3;1])
= 1.0e-04 * -0.244731948340174 0.315866965061825
and this works too
f = @(x,c) (x(1).^2+c(1)).*x(2).^2; % The parameterized function.
c = [1.5 2]; % The parameter.
fminsearch(@(x) f(x,c),[0.3;1])
.
If you manage in any of these ways, would you please consider marking my answer as accepted answer?
thanks for attention
John BG

Darshit Mehta on 16 Mar 2017
fminsearch can only take functions with one input argument (it can be a vector though). So your sum_A needs to be rewritten. Also, you don't need 'i' as an argument for sum_A in the example you have provided.
Albert on 17 Mar 2017
Dear Mehta Thank you for you answer. I know I do not really need to use i in the above example. However, in the real life, i=1:1:300, then how are you going to deal with this? To me, sum_A=@(i,x) sum(fun(2:300,x)); just has one input argument which is x since i has been assigned with a specific number. Could you provide any further precious help please?
Thank you

Albert on 17 Mar 2017
Thank everyone.
I finally got it work. Here is my solution. I apologize for my vague question. It was my bad to introduce i as a number instead of an index.
n=100;
dz=1;
A=@(X) sum((X(3:n)-2*X(2:n-1)+X(1:n-2)).^2/(dz)^3);
[exit,fval,exitflag]=fminsearch(A,initial);
Thanks the support and power of MATLAB community.

John BG on 16 Mar 2017
Hi Albert
1.
I have changed the syntax so it doesn't return error, could you please be so kind to confirm that this way is how you need to further process your data?
fun = @(x,i) 100*(x(2)-x(1)^2)^2+(i- x(1))^2;
% MATLAB comment // I have to do this summation in my real code
x0 = [-1.2,1];
N=10
sum_A=0
for k=1:1:N
sum_A=sum_A+fun(x0,k)
end
2. I don't know if the following is what you need for the fminsearch, again I make it avoid error, please confirm it me be of use in your code
ni=[-10:.01:10]
fun2= @(x,s) 100*(x(2)-x(1)^2)^2+(s- x(1))^2;
for s=1:1:numel(ni)
xmin = fminsearch(@(x) fun2(x,ni(s)),x0);
end
John BG
Albert on 19 Mar 2017
Hi, John
I appreciate your help very much.
I apologize for any ambiguity in my description of question. Please allow me to restate my question: I am trying to find a curve in 3D space with the minimum integral curvature in the vicinity of a given curve instead of trying to find a single point with minimum curvature in a image.
My method is to discretize the given curve into 200 points, then take the summation of curvature at each point. Also the curvature at a point of 3D curve is not equal to the Laplacian value at that point. It is K=|(dr/ds) x (d^2r/ds^2)|/|(dr/ds)|^3 where r=[x(s);y(s);z(s)]; Then use fminsearch to find new 200 points with minimum integral curvature.
At the very beginning, I was trying pass a summation of function handles to fminsearch (a curvature at a given point was a function handle; 200 points yielded 200 curvatures and 200 function handles ), it didn't work and always threw me an error. Then I switched the order. I took summation of the curvature at the 200 points first and then pass this single function handle to fminsearch instead of passing summation of function handles. It woks in this way.
Even though it works now, honestly I don't understand why fminsearch doesn't accept summation of function handles. I don't see much difference between these two methods.
Thank John very much.