Failure in initial objective function evaluation. PARTICLESWARM cannot continue.

19 次查看(过去 30 天)
%Please help me with this problem, here is my code
fun = @(X1Fit,X2Fit)sum(abs(YFit-42.2573 + 0.2109.*X1Fit(1) - 0.3602.*X2Fit(2).^2));
rng default % For reproducibility
nvars = 2;
lb=[25 -3.38];% %lower bounds of variables
ub=[85 5.38];%%upper bounds of variables
options = optimoptions('particleswarm','SwarmSize',100,'HybridFcn',@fmincon);
[x,fval,exitflag,output] = particleswarm(fun,nvars,lb,ub,options)
fsurf(@(X1Fit,X2Fit)(YFit - 42.2573 + 0.2109.*X1Fit - (0.3602)*X2Fit.^2))
%Error
Not enough input arguments.
Error in tut1>@(X1Fit,X2Fit)sum(abs(YFit-42.2573+0.2109.*X1Fit(1)-0.3602.*X2Fit(2).^2))
Error in particleswarm>makeState (line 694)
firstFval = objFcn(state.Positions(1,:));
Error in particleswarm>pswcore (line 169)
state = makeState(nvars,lbMatrix,ubMatrix,objFcn,options);
Error in particleswarm (line 151)
[x,fval,exitFlag,output] = pswcore(objFcn,nvars,lbRow,ubRow,output,options);
Error in tut1 (line 23)
[x,fval,exitflag,output] = particleswarm(fun,nvars,lb,ub,options)
Caused by:
Failure in initial objective function evaluation. PARTICLESWARM cannot continue.

采纳的回答

Walter Roberson
Walter Roberson 2019-8-15
particleswarm never passes two separate parameters to an objective function. You need to pack all of the parameters together into a single vector. You can separate them into separate variables inside the function.

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by