Index exceeds matrix dimensions.

Dear sirs
i have problem with this code . can any one help me
error
Index exceeds matrix dimensions.
Error in Untitled3 (line 47)
CTI1 = A(1,1)*x(1)+A(1,6)*x(6)
the code
%To minimize our fitness function using the ga function, we need to pass in a function
% handle to the fitness function as well as specifying the number of variables as the
% second argument. Lower and upper bounds are provided as LB and UB respectively. In
% addition, we also need to pass in a function handle to the nonlinear constraint function.
tic
clear
clc
ObjectiveFunction = @simple_fitness;
nvars = 14; % Number of variables
% Lower bound for (x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14)
LB = [0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05 0.05];
% Upper bound for (x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14)
UB = [1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 1.1 ];
A=zeros(20,14); b=zeros(20,1);
A(1,1)=+2.174; A(1,6)=-1.777; b(1)=-0.3; %first row
A(2,1)=-3.555; A(2,2)=+1.159; b(2)=-0.3; %second row
A(3,2)=+1.159; A(3,7)=-2.137; b(3)=-0.3; %third row
A(4,2)=-1.272; A(4,3)=+2.880; b(4)=-0.3; %fourth row
A(5,3)=-3.593; A(5,4)=+3.043; b(5)=-0.3; %fifth row
A(6,4)=-3.818; A(6,5)=+1.402; b(6)=-0.3; %sixth row
A(7,5)=-1.650; A(7,6)=+1.519; b(7)=-0.3; %seventh row
A(8,5)=-1.650; A(8,7)=+1.606; b(8)=-0.3; %eighth row
A(9,6)=+1.519; A(9,14)=-2.143; b(9)=-0.3; %nighth row
A(10,7)=+1.606; A(10,13)=-2.629; b(10)=-0.3; %tenth row
A(11,7)=-2.137; A(11,8)=+1.806; b(11)=-0.3; %eleventh row
A(12,8)=+1.806; A(12,9)=-1.622; b(12)=-0.3; %tweleventh row
A(13,9)=+1.948; A(13,10)=-3.605; b(13)=-0.3; %thirteenth row
A(14,10)=+2.753; A(14,11)=-3.935; b(14)=-0.3; %fourteenth row
A(15,11)=+3.105; A(15,12)=-1.287; b(15)=-0.3; %fiveteenth row
A(16,12)=+1.181; A(16,13)=-2.629; b(16)=-0.3; %sixteenth row
A(17,12)=+1.181; A(17,14)=-2.143; b(17)=-0.3; %seventennth row
A(18,8)=-2.232; A(18,13)=+1.816; b(18)=-0.3; %eighteenth row
A(19,1)=-3.555; A(19,14)=+1.608; b(19)=-0.3; %nighnteenth row
A(20,9)=-1.622; A(20,14)=+1.608; b(20)=-0.3; %twinty row
Aeq=[];
beq=[];
nonlcon=[];
IntCon=[];
rng default % For reproducibility
options=gaoptimset('plotfcns',{'gaplotgenealogy','gaplotselection', 'gaplotbestf' ...
,@gaplotscorediversity});
options= gaoptimset(options,'Tolfun',1e-6,'TolCon',1e-3,'stallGenlimit',50);
options= gaoptimset(options,'MutationFcn',{@mutationadaptfeasible});
options=gaoptimset(options,'PopulationSize',200);
options=gaoptimset(options,'PopulationType','doubleVector');
options=gaoptimset(options,'CrossoverFcn',{@crossoverarithmetic});
options=gaoptimset(options,'Display', 'iter');
[x,fval,exitflag,output,population,scores] ...
= ga(ObjectiveFunction,nvars,A,b,Aeq,beq,LB,UB,nonlcon,IntCon,options)
toc
CTI1 = A(1,1)*x(1)+A(1,6)*x(6)
CTI2 = A(2,1)*x(1)+ A(2,2)*x(2)
CTI3 = A(3,2)*x(2)+ A(3,7)*x(7)
CTI4 = A(4,2)*x(2)+ A(4,3)*x(3)
CTI5 = A(5,3)*x(3)+ A(5,4)*x(4)
CTI6 = A(6,4)*x(4)+ A(6,5)*x(5)
CTI7 = A(7,5)*x(5)+ A(7,6)*x(6)
CTI8 = A(8,5)*x(5)+ A(8,7)*x(7)
CTI9 = A(9,6)*x(6)+ A(9,14)*x(14)
CT10 = A(10,7)*x(7)+ A(10,13)*x(13)
CTI11= A(11,7)*x(7)+ A(11,8)*x(8)
CTI12= A(12,8)*x(8)+ A(12,9)*x(9)
CTI13= A(13,9)*x(9)+ A(13,10)*x(10)
CTI14= A(14,10)*x(10)+ A(14,11)*x(11)
CTI15= A(15,11)*x(11)+ A(15,12)*x(12)
CTI16= A(16,12)*x(12)+ A(16,13)*x(13)
CTI17= A(17,12)*x(12)+ A(17,14)*x(14)
CTI18= A(18,8)*x(8)+ A(18,13)*x(13)
CTI19= A(19,1)*x(1)+ A(19,14)*x(14)
CTI20= A(20,9)*x(9)+ A(20,14)*x(14)

1 Comment

Please avoid asking the same questions several times. Usually duplicate questions are closed by the contributers.

Sign in to comment.

Answers (1)

per isakson
per isakson on 31 Dec 2018
Edited: per isakson on 31 Dec 2018
I've reproduced the error. The problem is that the variable, x, is empty when executions arrives at the line
CTI1 = A(1,1)*x(1)+A(1,6)*x(6)

5 Comments

That could happen if it was unable to find any points that fit within the constraints.
ga() returns
K>> exitflag
exitflag =
-2
K>> output
output =
struct with fields:
problemtype: 'linearconstraints'
rngstate: [1×1 struct]
generations: 0
funccount: 0
message: 'Could not find a feasible initial point.'
maxconstraint: []
Lesson: check the value of the exitflag
Stephan
Stephan on 31 Dec 2018
Edited: Stephan on 31 Dec 2018
This question was disscussed already - see link at the flag. The reason for the empty x is that there are no feasible points under the given constraints, bounds and objective function.
okay many thanks
I encountered the same problem. How did you solve it?Thanks

Sign in to comment.

Products

Release

R2017b

Asked:

on 30 Dec 2018

Community Treasure Hunt

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

Start Hunting!