I am calling function in GA Program but getting error

[c,d]=crossover(a,b)
Data of crossover.m file is as under % Crossover operator
function [c,d]=crossover(a,b)
nn=length(a)-1;
% generating a random crossover point
cpoint=floor(nn*rand)+1;
c=[a(1:cpoint) b(cpoint+1:end)];
d=[b(1:cpoint) a(cpoint+1:end)];
end
% end for crossover
when i run the program i get this in return from command prompt "Error: File: GA.m Line: 169 Column: 2 This statement is not inside any function. (It follows the END that terminates the definition of the function "bintodec".)"
Please tell where the problem

5 Comments

How you have named your file which has this function?
Dr. Silva i can send you complete code, in which i have called 3 functions, but its not working for me i am new in this group,should i email you code or attach it with comment
When I run the above function, I am not getting any error.
i am calling three functions crossover,mutate,mutation but each time i run GA.m file i get different errors
i have attached all the files plz check

Sign in to comment.

 Accepted Answer

Bilal Khurshid you are calling the functions crossover and mutate, after the end of function GA.m. You cannot call a functions after ending a function. You can write the definition of the functions after end of main function, but you cannot call it. Change the location of those two functions; place them where you want to call/use them. In the attached code, I have changed their position, that error is not showing out now.

3 Comments

Thanks Dr Silva i have replaced my GA.m file with your modified file, but still i am getting this error
>> [costs,schedule]=GA([2 3 1 4],2,12) Undefined function or variable 'pm'.
Error in mutation (line 5) if pm>rand,
Error in GA (line 93) [kk,popnew]=mutation(popsize*rand);
This is a error in your function mutation...you have used pm inside it and it is not sent/ defined inside the function.
I have added valve of pm and random operator is mutation function file, but still getting following error
Error in mutation (line 4) pm=0.05; % Mutation probability
Output argument "kk" (and maybe others) not assigned during call to "C:\Users\Bilal\Desktop\GA test\mutation.m>mutation".
Error in GA (line 93) [kk,popnew]=mutation(popsize*rand);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!