Can matlab pso toolbox optimize a @(x,y) function?

Hello everyboody! I'm trying to run the following code but I'm getting error:
f = @(x,y)sin(sqrt(x.^2+y.^2))/sqrt(x.^2+y.^2);
nvar = 2;
[x,fval] = particleswarm(f, nvar);
The error says:
Error using particleswarm>makeState (line 700)
Failure in initial objective function evaluation. PARTICLESWARM cannot continue.
Error in particleswarm>pswcore (line 170)
state = makeState(nvars,lbMatrix,ubMatrix,objFcn,options);
Error in particleswarm (line 152)
[x,fval,exitFlag,output] = pswcore(objFcn,nvars,lbRow,ubRow,output,options);
Error in Untitled (line 5)
[x,fval] = particleswarm(f, nvar);
Caused by:
Not enough input arguments.
Despite It says that there's "Not enough input arguments.", I saw many examples that was only necessary the function and the number of variables as input arguments.

 Akzeptierte Antwort

Matt J
Matt J am 19 Jan. 2017
Bearbeitet: Matt J am 19 Jan. 2017

0 Stimmen

It is your objective function that must take only a single argument,
f = @(p) sin(sqrt(p(1).^2+p(2).^2))/sqrt(p(1).^2+p(2).^2);

3 Kommentare

I changed the way you said and worked perfectly!Thanks! Is the expression:
f = @(p) sin(sqrt(p(1).^2+p(2).^2))/sqrt(p(1).^2+p(2).^2)
mathematically equivalent to the one below?
f = @(x,y)sin(sqrt(x.^2+y.^2))/sqrt(x.^2+y.^2);
I tried to plot the new expression using fsurf(), but I was unsuccessful. Is there a way to do it?
Yes, it is equivalent. For plotting purposes, you can obtain a two- argument equivalent as follows,
g=@(x,y) f([x,y]);
Ok, thanks!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by