Problem with understanding the error
이전 댓글 표시
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"?
답변 (2개)
Image Analyst
2015년 4월 28일
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.
Star Strider
2015년 4월 28일
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
Ace_ventura
2015년 4월 29일
Star Strider
2015년 4월 29일
What I’m wondering is how the negative index gets created. We need to see the code that’s throwing the error.
카테고리
도움말 센터 및 File Exchange에서 Genetic Algorithm에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!