Problem with understanding the error

2 views (last 30 days)
Ace_ventura
Ace_ventura on 28 Apr 2015
Commented: Star Strider on 29 Apr 2015
I have the following code
function x = mapvariables(x)
data=xlsread('newsections.xlsx'); %reading data from excel file
allx1_2=data(:,1)'; %Extracting all the serial numbers
x(1) = allx1_2(x(1));
x(2) = allx1_2(x(2));
allx1_2 is a 1*65 row vector which contains numbers in serial i.e. 1,2,3..65. I am running a genetic algorithm and my variable 'x' takes the value from this discrete set 'allx1_2'. Another file that contains Constraints is:
function [c, ceq] = ga_constraints(x,Dos)
Lc=5;Lb=12; %in metre
E = 2e8; % Young's modulus in kN/m^2
deltaMax =Lb/325 ; % Maximum end deflection in m
%sigmaMax =130e3 ; % Maximum stress in each section of the beam in kN/m2
lamcolmax=250; % Max slenderness limits
lambeammax=180;
x=mapvariables(x);
stress = Dos.ch-1;
% Deflection of the stepped cantilever
deflection = Dos.disp - deltaMax;
% Slenderness ratio constraints
Sl_Ratio = [
Lc - lamcolmax*Dos.rcol;...
Lb - lambeammax*Dos.rbeam];
% All nonlinear constraints
c = [stress;deflection;Sl_Ratio];
% No equality constraints
ceq = [];
%fun=@(Dos)ga_constraints(x,Dos); %Anonymous function which will be called
Dos is another function which generates constraints.I have checked Dos and it works fine.My constraints are non linear.when I run GA as
rng(1,'twister')
[x fval exitflag]=ga(@final_cost,2,[],[],[],[],[],[],@ga_constraints,[1 2])
  • It gives following error ; *Error using ga (line 342)Attempted to access allx1_2(-1); index must be a positive integer or logical.
Caused by: Failure in initial user-supplied nonlinear constraint function evaluation. I'm not getting why it is taking (-1) ???? SORRY FOR THE LONG POST..I JUST NEED TO KNOW WHERE AM I GOING WRONG? what is the "Caused by"?

Answers (2)

Image Analyst
Image Analyst on 28 Apr 2015
By looking at this: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/ you will be able to discover why you're passing an index of -1 to allx1_2. Indexes start at 1, not -1 or 0 or any arbitrary number. See the FAQ.
In the lines
x(1) = allx1_2(x(1));
x(2) = allx1_2(x(2));
has x been defined yet? It comes in via the argument list. So why are you having either x(1) or x(2) be -1? That's why you're getting an error. x must contain integers like 1,2,3,4,5,..... not -1 (like you have) and not 0 or any negative number.
  1 Comment
Ace_ventura
Ace_ventura on 29 Apr 2015
x has been defined as function. It has variables x(1) and x(2). TO make sure x(1) takes positive integer ,I have formed the set allx1_2 which contains[1 2 3....65]. x(1) =allx1_2(x(1)) will then map x(1) from this set and should take any positive value. I don't understand from where-1 is coming?

Sign in to comment.


Star Strider
Star Strider on 28 Apr 2015
Is there more to the error message, like a listing of the exact line throwing the error?
What does this line (and the function it calls)
x=mapvariables(x);
do? Could that be throwing the error? I don’t see where you use ‘x’ anywhere else.
  2 Comments
Ace_ventura
Ace_ventura on 29 Apr 2015
x has been defined as function. It has variables x(1) and x(2). TO make sure x(1) takes positive integer ,I have formed the set allx1_2 which contains[1 2 3....65]. x(1) =allx1_2(x(1)) will then map x(1) from this set and should take any positive value. I am applying genetic algorithm on this problem. x(1) ,x(2) are my variables. x=mapvariables(x) directs the code to this function so that it takes value from the particular discrete set.
Star Strider
Star Strider on 29 Apr 2015
What I’m wondering is how the negative index gets created. We need to see the code that’s throwing the error.

Sign in to comment.

Categories

Find more on Platform and License in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!