Borrar filtros
Borrar filtros

Assign Multiple variable error

1 visualización (últimos 30 días)
Zafer Sahin
Zafer Sahin el 21 de Abr. de 2022
Comentada: Mitch Lautigar el 21 de Abr. de 2022
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 comentarios
Zafer Sahin
Zafer Sahin el 21 de Abr. de 2022
Editada: Zafer Sahin el 21 de Abr. de 2022
Ok I understand thank you. But I wonder How I write "x1" and "x2" in be the same dimensions to not give error.
Mitch Lautigar
Mitch Lautigar el 21 de Abr. de 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.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by