fitnessfcn: Not enough input arguments.

2 views (last 30 days)
ANIL KUMAR K
ANIL KUMAR K on 6 Oct 2020
Commented: Star Strider on 6 Oct 2020
function Y = objfun(X)
% Y = fitness value
% X = variable vector (1*n)
% R = 1.994*r^-0.32*f^2.34*v^-0.01*d^0.18
% where r=[0.2:0.8], f=[0.2:0.6], v=[39.27:117.81], d=[0.2:0.8]
r=X(1,1); % variable 1
f=X(1,2); % variable 2
v=X(1,3); % variable 3
d=X(1,4); % variable 4
Y = 1.994*r^-0.32*f^2.34*v^-0.01*d^0.18;

Answers (2)

Ameer Hamza
Ameer Hamza on 6 Oct 2020
I think you are trying to run this function by pressing the Green Run button. It will not work because you can not specifying the value of X. Call it like this, from the command window or a script
X = [2 1 4 5]; % example values
objfun(X)

Star Strider
Star Strider on 6 Oct 2020
It would help to see your ga call.
When I run your function using this ga call:
Xest = ga(@objfun, 4)
it produces (in one run):
Xest =
-355.83 0.028458 181.05 -0.038601
Be certain that you are providing the correct value (4) for ‘nvars’ , or if you are using InitialPopulationMatrix that the matrix has 4 columns.
  2 Comments
ANIL KUMAR K
ANIL KUMAR K on 6 Oct 2020
Xest = ga(@objfun, 4)
Optimization terminated: average change in the fitness value less than options.TolFun.
Xest =
4.8324 0.0009 4.0761 0.0058
I got above values
Star Strider
Star Strider on 6 Oct 2020
The point is that your code no longer throws the error!
The values will likelly not be the same (at least exactly the same) on any specific run, unless the tolerances are decreased. (Use optimoptions to set them.)
It is generally necessary to use the values returned by ga that have the lowest fitness score (include the ‘fval’ output to return and display them) as starting parameter estimates for fminsearch or fminunc, or similar functions. Those functions will generally provide more precise parameter estimates, however the ga results with the lowest fitness scores are the best initial values to provide to them.
If my Answer helped you solve your problem, please Accept it!
.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!