Search code examples
matlabgenetic-algorithm

Too many input arguments" error in genetic algorithm


I want to minimize a very simple function.

function z=f(x)
    f=(x(1)-2)^2+(x(2)-1)^2
end

and I write this in matlab command window:

>> [x fval]=ga(@f,2)

But I get this error:

??? Error using ==> ga

Too many input arguments.

Any suggestions to fix this problem?


Solution

  • If you have multiple ga.m, the build in and your own ga.m in C:\Users\student\Documents\MATLAB\ga.m, matlab uses

    1. Files in the current directory
    2. Files in the order they are placed on path

    Obviously, your ga.m is first in order, which causes Matlab to call it instead of the buildin. Either fix the path settings, or delete C:\Users\student\Documents\MATLAB\ga.m