How to solve error for Index exceeds matrix dimensions

I am trying to optimize the PID gains using jaya algorithm objective funcion is at the end But i am getting this error
Index exceeds matrix dimensions.
Error in Jaya_test (line 28) if(fnew(i)<f(i))
if true
function Jaya()
RUNS=1;
runs=0;
while(runs<RUNS)
pop=50; % population size
var=3; % no. of design variables
maxFes=1000;
maxGen=floor(maxFes/pop);
mini=[0 0 0];
maxi=[5 5 5];
[row,var]=size(mini);
x=zeros(pop,var);
for i=1:var
x(:,i)=mini(i)+(maxi(i)-mini(i))*rand(pop,1);%%change
end
ch=1;
gen=0;
f=myobj(x)
while(gen<maxGen)
xnew=updatepopulation(x,f);
xnew=trimr(mini,maxi,xnew);
fnew=myobj(xnew)
for i=1:pop
if(fnew(i)<f(i))
x(i,:)=xnew(i,:);%%change
f(i)=fnew(i);
end
end
disp('%%%%%%Final population%%%%%%%');
disp([x,f]);
fnew=[];xnew=[];
gen=gen+1;
fopt(gen)=min(f);
end
runs=runs+1;
[val,ind]=min(fopt);
Fes(runs)=pop*ind;
best(runs)=val;
end
function[z]=trimr(mini,maxi,x)
[row,col]=size(x);
for i=1:col
x(x(:,i)<mini(i),i)=mini(i);
x(x(:,i)>maxi(i),i)=maxi(i);
end
z=x;
end
function [xnew]=updatepopulation(x,f)
[row,col]=size(x);
[t,tindex]=min(f);
Best=x(tindex,:);
[w,windex]=max(f);
worst=x(windex,:);
xnew=zeros(row,col);
for i=1:row
for j=1:col
r=rand(1,2);
xnew(i,j)=x(i,j)+r(1)*(Best(j)-abs(x(i,j)))-r(2)*(worst(j)-abs(x(i,j)));
end
end
end
function [f]=myobj(x)
assignin('base', 'Kp', x(1));
assignin('base', 'Ki', x(2));
assignin('base', 'Kd', x(3));
sim('PID_TUNING_2.slx');
f = sum(abs(t.*E)).*1e-4;
end

4 Commenti

Torsten
Torsten il 23 Gen 2018
Modificato: Torsten il 23 Gen 2018
"f" and "fnew" are scalars in your code, not arrays.
So what do you try to access when you use f(i) and fnew(i) in the for-loop ?
Best wishes
Torsten.
f and fnew becomes array after calling the function myobj??
No. Since you take the sum, both remain scalars.
Best wishes
Torsten.

Accedi per commentare.

Risposte (0)

Categorie

Scopri di più su MATLAB in Centro assistenza e File Exchange

Richiesto:

il 23 Gen 2018

Commentato:

il 24 Gen 2018

Community Treasure Hunt

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

Start Hunting!

Translated by