Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Optimization toolbox error

Subject: Optimization toolbox error

From: mauro Gaggero

Date: 21 Jan, 2009 14:29:01

Message: 1 of 3

Hi, while using fmincon function of the Optimization Toolbox in Matlab r2008b I got the following error:
--------------------------------------------------------------------
??? Subscript indices must either be real positive integers or logicals.
Error in ==> qpsub>eqnsolv at 916
        ACTSET = A(ACTIND,:);
Error in ==> qpsub at 155
    [Q,R,A,B,X,Z,how,ACTSET,ACTIND,ACTCNT,aix,eqix,neqcstr,ncstr, ...
Error in ==> nlconst at 668
        [SD,lambda,exitflagqp,outputqp,howqp,ACTIND] ...
Error in ==> fmincon at 728
    [X,FVAL,LAMBDA,EXITFLAG,OUTPUT,GRAD,HESSIAN]=...
Error in ==> optimizerFmincon at 48
        [uopt(:,i), J(i), exitfl(i)] = fmincon(@(u) costFunction(u, x, d, A, B, C, D, Ncontrols, Nsell, xaxis, cost,
        t, T), u0, [], [], [], [], Umin-Umin, Umax, @(u) constraints(u, x, d, Umin, freq,
Error in ==> simulationSC at 25
    [controls, Jopt, exitflag] = optimizerFmincon(x, d, A, B, C, D, k, T, Umin, Unominal, Umax, freq, Ncontrols,
    Nsell, xaxis, cost, optimizationTrials);
Error in ==> mainSC at 56
[x, u] = simulationSC(d, x0, xmax, Umin, Unominal, Umax, freq, A, B, C, D, Ncontrols, Nsell, xaxis, cost, T, Tfin,
optimizationTrials);
--------------------------------------------------------------------

The variable ACTIND in qpsub.m takes on negative values and thus the instruction ACTSET = A(ACTIND,:); fails. By adding ACTIND = max(ACTIND, 1); the error disapperars.

I got this error while solving many optimization problems with inequality and equality constraints one after the other. The error appears in different points when I lauch again the simulation. How can I solve my problem? Did I make something wrong or Matlab has a bug?

Thanks a lot.
Mauro

I post the call of fmincon:
[uopt(:,i), J(i), exitfl(i)] = fmincon(@(u) costFunction(u, x, d, A, B, C, D, Ncontrols, Nsell, xaxis, cost, t, T), u0, [], [], [], [], Umin-Umin, Umax, @(u) constraints(u, x, d, Umin, freq, A, B, D, Ncontrols, Nsell, t, T), options);

the cost function:
function retval = costFunction(u, x, d, A, B, C, D, Ncontrols, Nsell, xaxis, cost, currentInstant, T)

retval = 0;
one = ones(1, Ncontrols);
for k=currentInstant:currentInstant+T-1
    retval = retval + C*x(:,k) + one*getTransferCost(u(1+(k-currentInstant)*Ncontrols:(k-currentInstant+1)*Ncontrols), Ncontrols, xaxis, cost);
    x(:,k+1) = A*x(:,k) + B*u(1+(k-currentInstant)*Ncontrols:(k-currentInstant+1)*Ncontrols) + D*d(:,k);
end

the constraints function:
function [c, ceq] = constraints(u, x, d, Umin, freq, A, B, D, Ncontrols, Nsell, currentInstant, T)

ceq = zeros((Ncontrols-Nsell)*T, 1);
dimX = size(x, 1);
c1 = zeros(dimX*T, 1);
c2 = zeros((Ncontrols-Nsell)*T, 1);
c = zeros(dimX*T+(Ncontrols-Nsell)*T, 1);
for k=currentInstant:currentInstant+T-1
    for i=1:Ncontrols-Nsell
        if(mod(k, freq(i))~=0)
            ceq((k-currentInstant)*(Ncontrols-Nsell)+i) = u(Nsell*(k-currentInstant)+(k-currentInstant)*(Ncontrols-Nsell)+i);
        else
            c2((k-currentInstant)*(Ncontrols-Nsell)+i) = Umin(i) - u(Nsell*(k-currentInstant)+(k-currentInstant)*(Ncontrols-Nsell)+i);
        end
    end
    x(:,k+1) = A*x(:,k) + B*u(1+(k-currentInstant)*Ncontrols:(k-currentInstant+1)*Ncontrols) + D*d(:,k);
    c1(1+(k-currentInstant)*dimX:(k-currentInstant+1)*dimX,1) = -x(:,k+1);
end
c = [c1;c2];

Subject: Optimization toolbox error

From: Marcelo Marazzi

Date: 22 Jan, 2009 16:01:46

Message: 2 of 3

Mauro,

 From inspecting the code that you posted I was not able to
see anything obviously wrong that could cause the error.

It would be useful to be able to run your code to reproduce
the problem. If it's possible, I suggest that you send your
code to tech support.

If this is not possible, can you post the values of all the
inputs to qpsub() during the call where the error occurs?

-Marcelo

mauro Gaggero wrote:
> Hi, while using fmincon function of the Optimization Toolbox in Matlab r2008b I got the following error:
> --------------------------------------------------------------------
> ??? Subscript indices must either be real positive integers or logicals.
> Error in ==> qpsub>eqnsolv at 916
> ACTSET = A(ACTIND,:);
> Error in ==> qpsub at 155
> [Q,R,A,B,X,Z,how,ACTSET,ACTIND,ACTCNT,aix,eqix,neqcstr,ncstr, ...
> Error in ==> nlconst at 668
> [SD,lambda,exitflagqp,outputqp,howqp,ACTIND] ...
> Error in ==> fmincon at 728
> [X,FVAL,LAMBDA,EXITFLAG,OUTPUT,GRAD,HESSIAN]=...
> Error in ==> optimizerFmincon at 48
> [uopt(:,i), J(i), exitfl(i)] = fmincon(@(u) costFunction(u, x, d, A, B, C, D, Ncontrols, Nsell, xaxis, cost,
> t, T), u0, [], [], [], [], Umin-Umin, Umax, @(u) constraints(u, x, d, Umin, freq,
> Error in ==> simulationSC at 25
> [controls, Jopt, exitflag] = optimizerFmincon(x, d, A, B, C, D, k, T, Umin, Unominal, Umax, freq, Ncontrols,
> Nsell, xaxis, cost, optimizationTrials);
> Error in ==> mainSC at 56
> [x, u] = simulationSC(d, x0, xmax, Umin, Unominal, Umax, freq, A, B, C, D, Ncontrols, Nsell, xaxis, cost, T, Tfin,
> optimizationTrials);
> --------------------------------------------------------------------
>
> The variable ACTIND in qpsub.m takes on negative values and thus the instruction ACTSET = A(ACTIND,:); fails. By adding ACTIND = max(ACTIND, 1); the error disapperars.
>
> I got this error while solving many optimization problems with inequality and equality constraints one after the other. The error appears in different points when I lauch again the simulation. How can I solve my problem? Did I make something wrong or Matlab has a bug?
>
> Thanks a lot.
> Mauro
>
> I post the call of fmincon:
> [uopt(:,i), J(i), exitfl(i)] = fmincon(@(u) costFunction(u, x, d, A, B, C, D, Ncontrols, Nsell, xaxis, cost, t, T), u0, [], [], [], [], Umin-Umin, Umax, @(u) constraints(u, x, d, Umin, freq, A, B, D, Ncontrols, Nsell, t, T), options);
>
> the cost function:
> function retval = costFunction(u, x, d, A, B, C, D, Ncontrols, Nsell, xaxis, cost, currentInstant, T)
>
> retval = 0;
> one = ones(1, Ncontrols);
> for k=currentInstant:currentInstant+T-1
> retval = retval + C*x(:,k) + one*getTransferCost(u(1+(k-currentInstant)*Ncontrols:(k-currentInstant+1)*Ncontrols), Ncontrols, xaxis, cost);
> x(:,k+1) = A*x(:,k) + B*u(1+(k-currentInstant)*Ncontrols:(k-currentInstant+1)*Ncontrols) + D*d(:,k);
> end
>
> the constraints function:
> function [c, ceq] = constraints(u, x, d, Umin, freq, A, B, D, Ncontrols, Nsell, currentInstant, T)
>
> ceq = zeros((Ncontrols-Nsell)*T, 1);
> dimX = size(x, 1);
> c1 = zeros(dimX*T, 1);
> c2 = zeros((Ncontrols-Nsell)*T, 1);
> c = zeros(dimX*T+(Ncontrols-Nsell)*T, 1);
> for k=currentInstant:currentInstant+T-1
> for i=1:Ncontrols-Nsell
> if(mod(k, freq(i))~=0)
> ceq((k-currentInstant)*(Ncontrols-Nsell)+i) = u(Nsell*(k-currentInstant)+(k-currentInstant)*(Ncontrols-Nsell)+i);
> else
> c2((k-currentInstant)*(Ncontrols-Nsell)+i) = Umin(i) - u(Nsell*(k-currentInstant)+(k-currentInstant)*(Ncontrols-Nsell)+i);
> end
> end
> x(:,k+1) = A*x(:,k) + B*u(1+(k-currentInstant)*Ncontrols:(k-currentInstant+1)*Ncontrols) + D*d(:,k);
> c1(1+(k-currentInstant)*dimX:(k-currentInstant+1)*dimX,1) = -x(:,k+1);
> end
> c = [c1;c2];
>

Subject: Optimization toolbox error

From: mauro Gaggero

Date: 26 Jan, 2009 09:06:23

Message: 3 of 3

Ok, thanks Marcelo. I'll prepare some .mat files with the input of qpsub when I have the error.

Mauro

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us