How to solve error for Index exceeds matrix dimensions

조회 수: 3 (최근 30일)
madhur aggarwal
madhur aggarwal 2018년 1월 23일
댓글: madhur aggarwal 2018년 1월 24일
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
Torsten
Torsten 2018년 1월 23일
No. Since you take the sum, both remain scalars.
Best wishes
Torsten.
madhur aggarwal
madhur aggarwal 2018년 1월 24일
thank you @torsten

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by