Not Enough Input Argument Error

3 views (last 30 days)
Hello everyone, I am trying to solve a problem using optimization toolbox. The solver i am using is Genetic algorithm solver. I do not understand but i am keep getting this error in my objective function section. I am using live editor and there is a local function section and i wrote my objective function there but keep getting this error. My data is showing in the workspace but objective function seem to be not recgonizing any of the data i have. Here is the objective function i wrote. Can you help me to solve it please?
thank you in advance!
PS: I wrote temp becasue it gave me index error before so tried to solve it this way. Now i am having input argument error.
function z = objectivefunct(ai,rij,xij)
temp=reshape(xij,36,36);
for i = 1:36
for j = 1:36
z = (-1)*(ai(i,1)*temp(i,j)*rij(i,j));
end
end
end

Accepted Answer

Andreas Apostolatos
Andreas Apostolatos on 16 Mar 2022
Edited: Andreas Apostolatos on 16 Mar 2022
Hi,
It is yet not clear what kind of error you are receiving, since you have not shared it with us. However I understand that you are trying to use the 'ga' function in MATLAB and, that you pass as first argument a handle to the function 'objectivefunct' you defined above. Please note, that the function handle that represents the objective function to be minimized should "accept a row vector of length nvars and return a scalar value." Please see the following link for more information,
In your case you are trying to pass three arguments to your objective function 'ai', 'rij', 'xij' which is obviously not allowed. If you want to pass additional arguments to your objective function which are defined in your base workspace, then you can use a wrapper, namely,
x = ga(@(ai) objectivefunct(ai,rij,xij), nvars)
What happens here is, that function 'ga' "sees" only a function handle that receives one input argument 'ai' as input (assuming of course that this input argument contains your design variables) while the rest of the arguments are taken by their definitions in the base workspace.
Please note, that this question is already addressed in different threads, among which also in the following one,
I hope this helps.
Kind regards,
Andreas
  8 Comments
Azime Beyza Ari
Azime Beyza Ari on 20 Mar 2022
hey Andreas,
So i tried writing my matrices as a row vector but still getting an error! If you have time can you have a look in to the file i attahced in the last comment i wrote in 18 march.
sorry if i am taking your time. But really need assistance! I am stuck and can not do it without any assistance!
Thank you,
Best,
Beyza.
Andreas Apostolatos
Andreas Apostolatos on 21 Mar 2022
Hi Beyza,
Your design variables is a vector of nvars variables. which is set by the second input argument to function ga, see the following lines of code:
[solution,z] = ga(objfun,nvars,[],[],[],[],zeros(nvars,1),...
ones(nvars,1),[],[],options);
According to the documentation, the array of the design variables is a row vector with its dimension being equal to nvars, see the following documentation page for more information,
where it is mentioned:
"Number of variables, specified as a positive integer. The solver passes row vectors of length nvars to fun."
Thus, this is obviously a column-vector, but you are trying to access some elements in the second row within your function handle representing your objective function, see the following lines of code,
function z = objectivefunc(vars,rij,ai)
xij = vars(1);
for i = 1:36
for j = 1:36
z = (-1)*(vars(i,j)*rij(i,j)*ai(i,1));
end
end
end
I would recommend you to revise the reason why you are trying to access more rows of array vars, namely vars(i,j), since your design variable vector has only one vector as said above.
You need to first understand the error that MATLAB asserts and then based on that try to debug to better understand the code and how your code corresponds to your mathematical modeling. The best way to do this, is by adding break points, see the following screenshot,
By placing a breakpoint at line 148 and then hovering the mouse pointer over the variable vars I can see, that this is row-vector. In your nested loop however the index i runs from 1 to 36, which triggers the error, as no other value for index i than 1 can be accessed.
If you have further questions please consider creating a new MATLAB Answers thread.
I hope this information helps you.
Kind regards,
Andreas

Sign in to comment.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!