NaN constraint problem

139 views
Skip to first unread message

Kevin Tanghe

unread,
Nov 2, 2013, 11:27:53 AM11/2/13
to yal...@googlegroups.com
Hi,

I'm new to Yalmip and got the following error: ??? Error using ==> compileinterfacedata at 963 You have NaNs in your constraints!
First of all, I have read http://users.isy.liu.se/johanl/yalmip/pmwiki.php?n=Extra.NANInModel and I tried to find similiar questions in the forum but that didn't help me.

This is the problem: I want to compute the optimal values for a (1 x t) vector called Tlb, subjected to some constraints. From Tlb I need to compute 2 other variables: a (n x t) matrix T and a (1 x t) vector Tlsys. These come up in the objective function and in the constraints.

Here's my code:
C = [];
Tlb = sdpvar(1,length(t));
Tlsys = sdpvar(1, length(t));
T
= [Tinit, zeros(n,length(t)-1)];
C
= [C,Tlb(1) == Tlbinit];
C
= [C,Tlsys(1)==Tlsysinit];
b
= zeros(n,1);
for j = 2:length(t)
    b
(1) = -rho*Ac*dx*cp*T(1,j-1)/dt - (double(Tlb(j-1))*cp*mbnom+U*Ap*Tamb);
   
for i = 2:n-1
        b
(i) = -rho*Ac*dx*cp/dt*T(i,j-1) - U*Ap*Tamb;
   
end
    b
(n) = -rho*Ac*dx*cp/dt*T(n,j-1) -(U*Ap*Tamb+msys*cp*double(Tlsys(j-1)));
   
    T
(:,j) = A\b;
    C
= [C, Tlsys(j) == T(1,j-1)-Qsys(j-1)/msys/cp];
    C
= [C , (Tlb(j)-T(n,j-1))*mbnom*cp <=Qbnom];
   
end
C
= [C, Tlb(:)>=70];
C
= [C ,Tlsys(:) >=20];

f
= sum((Tlb(2:end)-T(n,1:end-1))*mbnom*cp.*c(1:end-1)*dt);
options
= sdpsettings('solver', 'sdpt3');
solvesdp
(C, f, options);


The problem is situated in the lines where I compute b(1) and b(n). If I remove the terms with double(Tlb(j)) and double(Tlsys(j)) I don't get the NaN number anymore. What can I do?

Johan Löfberg

unread,
Nov 2, 2013, 12:36:01 PM11/2/13
to yal...@googlegroups.com
You cannot put an sdpvar in a variable defined as a double (MATLAB limitation is class logic)

a = zeros(2,1);
sdpvar y
a(1) = y
a(2) = y

yields nans (MATLAB places double(y) in the elements)

you should create a using concatenations the loop instead, e.g,
a = []
a = [a y]
a = [a y]

Kevin Tanghe

unread,
Nov 3, 2013, 5:58:17 AM11/3/13
to yal...@googlegroups.com
Thank you very much, now it works.

Some further questions:
  1. I had to specifically declare
    T = sdpvar(n, length(t));
    C
    = [C, T(:,1)==Tinit];
    If I didn't, I again got the NaN error. Is that normal?
  2. How can I select the best solver for this problem?

Johan Löfberg

unread,
Nov 3, 2013, 9:41:58 AM11/3/13
to
1. So you now declared T as a big matrix and placed expressions on suitable columns (but you still have the same bug in your code when you define b). Sure, possible, but why not simply (concentrating on T now, maybe you are doing similiarly unnecessary stuff on other variables)

C = [];
Tlb = sdpvar(1,length(t));
Tlsys = sdpvar(1, length(t));

T
= Tinit;

C
= [C,Tlb(1) == Tlbinit];
C
= [C,Tlsys(1)==Tlsysinit];
for j = 2:length(t)

    b
= -rho*Ac*dx*cp*T(1,j-1)/dt - (double(Tlb(j-1))*cp*mbnom+U*Ap*Tamb);

   
for i = 2:n-1

        b
= [b;-rho*Ac*dx*cp/dt*T(i,j-1) - U*Ap*Tamb];
   
end
    b
= [b;-rho*Ac*dx*cp/dt*T(n,j-1) -(U*Ap*Tamb+msys*cp*double(Tlsys(j-1)))];
   
    T
= [T A\b];

    C
= [C, Tlsys(j) == T(1,j-1)-Qsys(j-1)/msys/cp];
    C
= [C , (Tlb(j)-T(n,j-1))*mbnom*cp <=Qbnom];
   
end

2. It surely is not sdpt3, since sdpt3 is a general SDP solver. Looks like you have a simple LP, so you should use an efficient LP solver (cplex, gurobi, mosek, etc etc)

BTW, you are doing MPC in a sort of elementwise fashion. Clean up the predictions, and you should be able to write it much more cleanly using matrices and simply follow all the MPC examples on the Wiki

Johan Löfberg

unread,
Nov 3, 2013, 9:49:27 AM11/3/13
to yal...@googlegroups.com
It is not clear if you want to define your MPC problem in explicit prediction form (optimize over inputs only and define prediction as affine maps of input) or if you want to optimize over states and inputs by declaring equalities (implicit form)

Is Tlb a state or input? The fact that it has an initial value tells me state, but there is no dynamical equation for Tlb in your model. 

Kevin Tanghe

unread,
Nov 3, 2013, 12:25:32 PM11/3/13
to yal...@googlegroups.com
I have sent you an e-mail with all the details. Thanks for you interest.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages