How to solve error for Index exceeds matrix dimensions

1 view (last 30 days)
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 Comments
Torsten
Torsten on 23 Jan 2018
No. Since you take the sum, both remain scalars.
Best wishes
Torsten.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!