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?!
답변 (5개)
Geoff
2012년 4월 1일
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
Geoff
2012년 4월 1일
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.
Image Analyst
2012년 4월 1일
0 개 추천
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?
hadi
2012년 4월 1일
0 개 추천
댓글 수: 4
Geoff
2012년 4월 1일
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.
hadi
2012년 4월 3일
Geoff
2012년 4월 3일
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
hadi
2012년 4월 5일
카테고리
도움말 센터 및 File Exchange에서 Set Optimization Options에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!