フィルターのクリア

How to pass summation of function handles to fminsearch

3 ビュー (過去 30 日間)
Albert
Albert 2017 年 3 月 16 日
回答済み: John BG 2017 年 3 月 20 日
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
John BG 2017 年 3 月 20 日
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

その他の回答 (3 件)

Darshit Mehta
Darshit Mehta 2017 年 3 月 16 日
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.
  1 件のコメント
Albert
Albert 2017 年 3 月 17 日
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
Albert 2017 年 3 月 17 日
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
John BG 2017 年 3 月 16 日
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
if you find this answer useful would you please be so kind to mark my answer as Accepted Answer?
To any other reader, please if you find this answer
please click on the thumbs-up vote link
thanks in advance
John BG
  5 件のコメント
John BG
John BG 2017 年 3 月 19 日
編集済み: John BG 2017 年 3 月 19 日
the 'curvature' of a 2D array is the 2nd derivative, aka Laplacian.
In MATLAB it's calculated with command
del2
But as you very well point out, it all ends up in numeric approximations: some people approximate the laplacian or curvature with 5 points instead of the 1 point you have implemented.
N points meaning N points EACH SIDE of the shifting point.
Let's say you have for instance the following input 2D:
A=imread('lines_count00.jpg');imshow(A)
.
.
Let me simplify to just Red layer, the lowest value of the curvature of A1 is
A1=A(:,:,1);L=del2(double(A1));
min(min(A))
=
-113
The location of such min, the flattest point, is:
find(L==min(min(L)))
=
339189
or what's the same:
[nx,ny]=ind2sub(size(L),find(L==min(min(L))))
nx =
389
ny =
701
So, having said all this, please choose, do you want to carry on your development with linear expressions like
fun = @(x,i)100*(x(2) - x(1)^2)^2 + (i - x(1))^2;
or
(x(i+1)-2x(i)+x(i-1))^2+ (y(i+1)-2y(i)+y(i-1))^2
or you would consider using the points generated with a meshgrid, and the surface function, along with the Laplacian, or a numeric approximation of del2, like
delsq
introduced in
.
John BG
Albert
Albert 2017 年 3 月 19 日
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.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by