Assign Multiple variable error
Show older comments
I write to code about optimization algorithm. When I use code block as a;
function z=Sphere(x)
z=sum(x.^2);
The codes run succesfully But I want to use two variables x and y. instead of this, I write
function z=Sphere(x,y)
z=sum(x.^2,y.^2);
It giver error:
Not enough input arguments.
If you help about source of eror codes, I will appreciate. Thanks
function []=DEA()
rng('default')
N=10;
F=0.8;
CR=0.9;
max_iter=5;
D=5;
low=-100;
high=100;
Fonksiyon='Sphere';
function z=Sphere(x)
z=sum(x.^2);
end
obj=zeros(1,N);
mutant=zeros(N,D);
for i=1:N
for j=1:D
pop(i,j)=low+rand()*(high-low);
end
obj(1,i)=eval(strcat(Fonksiyon,'(pop(i,:))'));
end
[minimum,min_indis]=min(obj);
bestParams=pop(min_indis,:);
mins=zeros(1,max_iter);
sayac=0;
iterasyon=1;
while (iterasyon<=max_iter)
for i=1:N
n1=fix(rand*N)+1;
while(i==n1)
n1=fix(rand*N)+1;
end
n2=fix(rand*N)+1;
while(i==n2||n1==n2)
n2=fix(rand*N)+1;
end
n3=fix(rand*N)+1;
while(i==n3||n1==n3||n2==n3)
n3=fix(rand*N)+1;
end
for j=1:D
mutant(i,j)=pop(n3,j)+(pop(n1,j)-pop(n2,j))*F;
if mutant(i,j)>high
mutant(i,j)=high;
end
if mutant(i,j)<low
mutant(i,j)=low;
end
end
trial=zeros(N,D);
j=fix(rand*D)+1;
for k=1:D
if rand<CR || k==j
trial(i,k)=mutant(i,k);
else
trial(i,k)=pop(i,k);
end
end
score=eval(strcat(Fonksiyon,'(trial(i,:))'));
if score<obj(1,i)
obj(1,i)=score;
pop(i,:)=trial(i,:);
end
if score<minimum
minimum=score;
bestParams=trial(i,:);
end
end
fprintf('Iterasyon=%d .... En Küçük Değer=%g\n',iterasyon,minimum);
mins(iterasyon)=minimum;
iterasyon=iterasyon+1;
end
end
5 Comments
Mitch Lautigar
on 21 Apr 2022
Error Reasoning: MATLAB's sum function sums along a specific axis (either by rows, or by columns) for an array of data. The 2nd input you are giving it (y.^2) is being treated as dimensions for the sum function to sum along.
- source: https://www.mathworks.com/help/matlab/ref/sum.html
Solution: Change your sphere function as I show below. Doing this makes it more generic. The function doesn't care if the input variable for the function call is "a" or "x" and you can just call the function twice in your programming.
old:
function z=Sphere(x)
z=sum(x.^2);
end
new:
function z=Sphere(a)
z=sum(a.^2);
end
I also do hope you are calling the function "Sphere" somewhere in your code, because it currently isn't visible in the code you called. Since the "sphere" function is only accessible in the DEA code, MATLAB considers it a local function and will ONLY work inside the DEA Function
Vatan Tosun
on 21 Apr 2022
Mitch Lautigar
on 21 Apr 2022
Your original notation was:
z=sum(x.^2,y.^2); which was why I gave my original answer encouraging you to use the variable 'a' to avoid confusion.
New Notation:
sum(x1^2+x2^2) should be written as "sum(x1.^2+x2.^2)" and both "x1" and "x2" should be the same dimensions. If not it will error out.
Vatan Tosun
on 21 Apr 2022
Edited: Vatan Tosun
on 21 Apr 2022
Mitch Lautigar
on 21 Apr 2022
"But I wonder How I write "x1" and "x2" in be the same dimensions to not give error."
x1 an x2 are inputs that you provide. You can either reshape the data (google MATLAB reshape) or you can add logic to sum the arrays regardless if they are the same size (see my code below).
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%------------------------------------------------------------------------%
%Get simple math out of way
x1_1 = x1.^2;
x2_1 = x2.^2;
%------------------------------------------------------------------------%
%If statement to handle variable length x arrays
%if x1_1 is longer than x2_1
if length(x1_1) > length(x2_1)
x1_1(1:length(x2_1) = x1_1(1:length(x2_1))+x2_1;
output_x = sum(x1_1);
%if x2_1 is longer than x1_1
elseif length(x1_1) < length(x2_1)
x2_1(1:length(x1_1) = x2_1(1:length(x1_1))+x1_1;
output_x = sum(x2_1);
%arrays are same length
else
output_x = sum(x1_1 + x2_1);
end
%end suggested code
%------------------------------------------------------------------------%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
This logic will add the values together and then sum them up regardless of the dimensions. I don't think their is a logic issue in my indexing, but you are welcome to try this food for thought and play around with it. If their is an issue, let me know.
Answers (0)
Categories
Find more on Agriculture in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!