Subscript indices must either be real positive integers or logicals

I am trying to optimize a function with nonlinear constraints. and the constraints function is as follows:
function [c,ceq]=mycon(X)
persistent Fgrrz Fgrlz xdata fourier
c=[min(fourier(X(1:11),xdata));1.8-min(fourier(X(1:11),xdata));max(fourier(X(12:22),xdata))-1.7;1.1-min(fourier(X(12:22),xdata));max(fourier(X(23:33),xdata));...
-3*pi/2+fourier(X(23:33),xdata);max(fourier(X(34:44),xdata));-min(fourier(X(34:44),xdata))-1.1;max(-Fgrrz);max(-Fgrlz)];
ceq=[];
end
but I am getting this error that "Subscript indices must either be real positive integers or logicals." what was going wrong?!

2 Comments

You omitted the most important part of the error message. On which line does the error occur?
Error in ==> mycon at 4
c=[min(fourier(X(1:11),xdata));1.8-min(fourier(X(1:11),xdata));max(fourier(X(12:22),xdata))-1.7;1.1-min(fourier(X(12:22),xdata));max(fourier(X(23:33),xdata));...
Error in ==> fmincon at 654
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});

Sign in to comment.

Answers (5)

How can fourier be a function when you just defined it as a persistent variable in your mycon function? I suspect you may be misusing the persistent keyword. Perhaps you meant to use global.
Perhaps you meant this:
function [c,ceq]=mycon( X, Fgrrz, Fgrlz, xdata )
And wrap it with an anonymous function for passing to your solver:
mycon_objective = @(x) mycon( x, Fgrrz, Fgrlz, xdata );

1 Comment

By the way, I am implying here that you remove the 'persistent' line entirely, and pass your data into your function. The wrapper allows this to happen, while maintaining the original function's calling syntax.

Sign in to comment.

Look at this part: fourier(X(1:11),xdata) What are fourier, X and xdata? First of all, fourier appears to be an array but it never appears to get assigned ever. None of the other 3 persistent variables seems to ever get assigned either. Then you're trying to look at the X,xdata elements but if X and xdata are not integers or logicals this doesn't make sense. How can you look at the 3.5th, 8.9th element of the array? It would need to be the 3,8 element or some similar integer designation.
By the way, why return ceq when it's always null?
is a function
function F= fourier(x,t)
F=x(1)+x(2)*sin(2*pi*t/111)+x(3)*cos(2*pi*t/111)+x(4)*sin(4*pi*t/111)+x(5)*cos(4*pi*t/111)+x(6)*sin(6*pi*t/111)+x(7)*cos(6*pi*t/111)+x(8)*sin(8*pi*t/111)+x(9)*cos(8*pi*t/111)+x(10)*sin(10*pi*t/111)+x(11)*cos(10*pi*t/111);
end
but when I am writing this in the MATLAB command window it works?!!
how could I use this function in the nonlinear constraint function do I need to use global syntax ?! no the other variables are inside the objective function so I just used them in the mycon again and they are all assigned.

4 Comments

The question is this: do you load and initialise xdata etc inside mycon? If so, use persistent. If not, don't. I avoid using global. Normally you load your data before running a solver. You then create the wrapper function I suggested (which can see your data, as long as you define it after the data is loaded), and then run your solver as usual. If you don't understand this, then you should probably post more code to show us exactly what you are doing.
basically what I am trying to ask is how could I call a function (Fourier) in the mycon function ?!
Define the function Fourier just as you have defined mycon. If the Fourier function is only relevant within mycon, then you can put it in the same file - no other functions will be able to see it. If you want it to be visible to other functions, define it in a file called Fourier.m
I already defined fourier.m but how could it be readable in the mycon function?! apparently the m file(fourier) has been defined in the folder which is not readable within the mycon function.

Sign in to comment.

basically I am using fmincon syntax to optimize the objective function which is :
function E=Energy(X)
persistent x3 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15n x16n x17n x18n x19n x20n x21n x22 x23n x24n x25 x26 main
Emt=0;
x1=[X(1:11)];
x2=[X(12:22)];
x4=[X(23:33)];
x5=[X(34:44)];
main;
E=Emt;
end
while the x are the variable to calculate the objective function and then the constraints defined on the mycon function.

Asked:

on 1 Apr 2012

Community Treasure Hunt

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

Start Hunting!