Info

This question is closed. Reopen it to edit or answer.

i want to know what is meant by double here pl answer

1 view (last 30 days)
Error using ga (line 342)
Undefined function 'constraints' for input arguments of type 'double'.
Error in main1 (line 11)
[x, fbest, exitflag] = ga(ObjectiveFunction, nvars, [], [], [], [],LB, UB,
ConstraintFunction,1:168, opts);
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation.
while my constraints function is
function [c, ceq]=constraints(x)

Answers (2)

mariam mughees
mariam mughees on 15 Nov 2015
whe i run my main file of GA it gives this error

Walter Roberson
Walter Roberson on 15 Nov 2015
In your code calling ga, you show ConstraintFunction . Did you code that as
ConstraintFunction = 'constraints';
and is your code
function [c, ceq]=constraints(x)
in the same file as your main1 ?
In that combination of circumstances, MATLAB would not be able to find the constraints function. When you specify a function name by a string, then MATLAB evaluates the string in the context of the base workspace, and the base workspace can only find named functions which have their own .m file, not functions that are in the same file as the calling code.
The way to fix this would be to change to
ConstraintFunction = @constraints;
  2 Comments
mariam mughees
mariam mughees on 16 Nov 2015
i have defined it in same way already ConstraintFunction = @constraints;
Walter Roberson
Walter Roberson on 16 Nov 2015
Is your constraints function in the same file as main1.m or is it in a separate file constraints.m ? Please show your code.

Community Treasure Hunt

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

Start Hunting!