Why this piece of code gives error?
2 views (last 30 days)
Show older comments
Commented: Steven Lord on 21 Apr 2022
MaxIter0=ceil(MaxNFE/((nPop_Initial+nPop_Final)/2)); % Approximate Maximum Iterations
xmin=[0 0 0 0];
xmax=[10 10 pi pi];
Steven Lord on 20 Apr 2022
Either make your CostFunction variable a function handle to the fun4sn0 function (which is the approach I'd prefer):
or use feval to evaluate the function by name.
Cost(i) = feval(CostFunction, Position(i, :));
Both of these assume that your function returns a scalar, since you're assigning its output into one element of the Cost variable.
Steven Lord on 21 Apr 2022
@Image Analyst That is another option, and is the easiest if you want to hard-code the cost function. I think that particular name put me in "this is likely going to be used in some form of optimization" mode and in that case (the "function function" scenario, see ode45, fminsearch, fzero, integral, etc.) to keep the code as a general purpose solver you wouldn't want to hard-code the function.
More Answers (1)
Image Analyst on 20 Apr 2022
CostFunction is a character array, not an actual function. Did you mean
Cost(i) = fun4sn0(Position(i,:));
Thorough discussion of the error in the FAQ:
Find more on Printing and Saving in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!