Index in position 2 exceeds array bounds (must not exceed 2).

7 views (last 30 days)
please can any one help me.
Why does the following function produce the error "Index in position 2 exceeds array bounds (must not exceed 2)"?
in line C=Coord(:,H(2))-Coord(:,H(1));
clear
close all
% Problem Statement
Npar = 10;
VarLow=ones(1,10)*0.1;
VarHigh =ones(1,10)*33.5;
%BBBC parameters
N=50; %number of candidates
MaxIter=100; %number of iterations
% initialize a random value as best value
XBest = rand(1,Npar).* (VarHigh - VarLow) + VarLow;
[FBest]=fitnessFunc(XBest);
GB=FBest;
t = cputime;
%intialize solutions and memory
X = zeros(N, Npar);
F = zeros(N, 1);
for ii = 1:N
X(ii,:) = rand(1,Npar).* (VarHigh - VarLow) + VarLow;
% calculate the fitness of solutions
F(ii) = fitnessFunc(X(ii,:));
end
%Main Loop
for it=1:MaxIter
%Find the centre of mass
%-----------------------
%numerator term
num=zeros(1,Npar);
for ii=1:N
for jj=1:Npar
num(jj)=num(jj)+(X(ii,jj)/F(ii));
end
end
%denominator term
den=sum(1./F);
%centre of mass
Xc=num/den;
%generate new solutions
%----------------------
for ii=1:N
%new solution from centre of mass
for jj=1:Npar
New=X(ii,:);
New(jj)=Xc(jj)+((VarHigh(jj)*rand)/it^2);
end
%boundary constraints
NewP=limiter(New,VarHigh,VarLow);
%new fitness
newFit=fitnessFunc(New);
%check whether the solution is better than previous solution
if newFit<F(ii)
X(ii,:)=New;
F(ii)=newFit;
if F(ii)<FBest
XBest=X(ii,:);
FBest=F(ii);
end
end
end
% store the best value in each iteration
GB=[GB FBest];
end
t1=cputime;
fprintf('The time taken is %3.2f seconds \n',t1-t);
fprintf('The best value is :');
XBest
FBest
figure(1)
plot(0:MaxIter,GB, 'linewidth',1.2);
title('Convergence');
xlabel('Iterations');
ylabel('Objective Function (Cost)');
grid('on')
function newP=limiter(P,VarHigh,VarLow)
newP=P;
for i=1:length(P)
if newP(i)>VarHigh
newP(i)=VarHigh;
elseif newP(i)<VarLow
newP(i)=VarLow;
end
end
end
function [WE,FBest]=fitnessFunc(X)
Coord=360*[2 1;2 0;1 1;1 0;0 1;0 0];
Con=[5 3;1 3;6 4;4 2;3 4;1 2;6 3;5 4;4 1;3 2];
Re=[0 0;0 0;0 0;0 0;1 1;1 1 ];
Load=zeros(size(Coord));
Load(2,:)=[0 -1e5];
Load(4,:)=[0 -1e5];
E=ones(1,size(Con,1))*1e7;
A=ones(1,10);
%Allowable Stress
TM=25000;%psi
%Allowable Displacement
DM=2;%inch
%Density
RO=0.1;%lb/in^3
w=size(Re);
S=zeros(3*w(2));
U=1-Re;f=find(U);
WE=0;
for i=1:size(Con,2)
H=Con(:,i);
C=Coord(:,H(2))-Coord(:,H(1)); *********problem here
Le=norm(C);
T=C/Le;
s=T*T';
G=E(i)*A(i)/Le;
Tj(:,i)=G*T;
e=[3*H(1)-2:3*H(1),3*H(2)-2:3*H(2)];
S(e,e)=S(e,e)+G*[s -s;-s s];
WE=WE+Le*D.A(i)*D.RO;
end
U(f)=S(f,f)\Load(f);
F=sum(Tj.*(U(:,Con(2,:))-U(:,Con(1,:))));
R=reshape(S*U(:),w);
R(f)=0;
TS=(((abs(F'))./A)/TM)-1;%Tension
US=abs(U')/DM-1;%Displacement
PS=sum(TS.*(TS>0));
PD=sum(sum(US.*(US>0)));
FBest=WE*(1+PS+PD)^2;% Penalty function
end

Answers (1)

Cris LaPierre
Cris LaPierre on 26 Jul 2020
Edited: Cris LaPierre on 27 Jul 2020
You define Coord to be a 5x2 array
Coord=360*[2 1;2 0;1 1;1 0;0 1;0 0];
Since Coord only has 2 columns, you can't ask for a column higher than 2 (the 2nd index value). However, in the line of code giving you an error (C=Coord(:,H(2))-Coord(:,H(1)), when i=1, then H(1)=5 and when i=2, then H(1)=H(2)=3.
  7 Comments
sura naji
sura naji on 27 Jul 2020
to calculate the net distance of the length of every member
Cris LaPierre
Cris LaPierre on 27 Jul 2020
Using the values given in your example above, what are the actual numeric values supposed to be? 15? 3.7?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!