MATLAB: How can i solve “Error using constrValidate” in genetic algorithm

costraintsgaMATLABnonlinearoptimization

I'm trying to minimaze a non linear function with non linear costraints with a ga (i have attached the files with the functions), but whenever i run the code i have the following errors:
Error using constrValidate (line 59)
Constraint function must return real value.
Error in gacommon (line 125)
[LinearConstr, Iterate,nineqcstr,neqcstr,ncstr] = constrValidate(NonconFcn, …
Error in ga (line 363)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, …
Error in main (line 28)
[x, fval] = ga(ObjFcn,nvars,A, b, Aeq, beq, [], [], CostraintFunction);
How can i fix that?
Thanks for helping

Best Answer

You have no upper or lower bounds, so all of your variables are permitted to be 0. In your constraint function, you divide by x(13) through x(18), so if any of those are 0 you get a division by 0, which leads to either +/- inf or to nan (you generate both.)
If you have no bounds, then your constraint function better be able to handle the all-zero input vector.
c=[0,000898*x(1)+0,000837*x(1)/x(13)-450;
What is the point of returning a column of 0's, and what is the point of adding 0 to the second column ? The above is the same as
c=[0, 000898*x(1)+0, 000837*x(1)/x(13)-450;
I have to wonder whether you intended decimal places instead of commas,
c=[0.000898*x(1)+0.000837*x(1)/x(13)-450;