Using tabulated data for nonlinear MPC using MATLAB

328 views
Skip to first unread message

Alessandro Maria Laspina

unread,
Aug 4, 2021, 11:28:14 AM8/4/21
to CasADi
Dear Joris,

I am trying to include parameters in my Dae that are a function of another parameter, which is a function of a constant and a state element of vector x as part of the function F(x,u).

In symbols, consider that x=[a,b,c], and c=P*a+b , where P is a parameter which is P=2*cR, where R in itself is a parameter for which we discrete data points given a value of G-a, where G is some constant. The expression  and dae itself is much more complicated than this so I have simplified it. 

How would R be called when building the dae expression in matlab? 

Joris Gillis

unread,
Aug 6, 2021, 7:40:41 AM8/6/21
to CasADi
Dear Alex,

Any fundamental quantity that is to be kept constant during a DAE integrator evaluation is considered a "parameter" (keyword p). If you build further on top of such parameter, we call that an "expression".

R would be called an "expression", and G a "parameter".
Once you have a,b,c and G defined as symbols, you would write:
R=G-a
P=2*c*R

You could also defined R as a Function (maybe interpolant?) and do:
P=2*c*R(G,a)
Where R(G,a) represents a symbolic call to the R Function.

Best regards,
  Joris

Alessandro Maria Laspina

unread,
Aug 7, 2021, 5:18:50 AM8/7/21
to CasADi
Thank you for the reply Joris. I have generated the needed interpolated expressions and as a result the dae from my problem. I have also passed any other constants using a cell structure PAR to the problem- it seems to accept it when constructing the functions and calling the opti function. However, when using solve, I run into the following error :

CasADi -  WARNING("solver:nlp_jac_fg failed: NaN detected for output g, at (row 1, col 1).") [.../casadi/core/oracle_function.cpp:265]
MESSAGE(sqpmethod): No regularity of sensitivities at current point.
      solver  :   t_proc      (avg)   t_wall      (avg)    n_eval
  nlp_jac_fg  |   2.03 s (  2.03 s)   2.03 s (  2.03 s)         1
       total  |   2.03 s (  2.03 s)   2.03 s (  2.03 s)         1
Error using casadi.Opti/solve (line 253)
Error in Opti::solve [OptiNode] at .../casadi/core/optistack.cpp:159:
.../casadi/core/function_internal.hpp:1229: Evaluation failed

Error in fun_cas_mpc (line 224)
sol=opti.solve();

Error in scrip_mpc_test (line 2)
[intg,dae]=fun_cas_mpc(PAR);


fun_cas_mpc is the function file that builds the casadi MPC controller, script_mpc_test is the script file, and PAR_val.mat contains all the constants. 


casadi_mpc_test.zip

Joris Gillis

unread,
Aug 9, 2021, 6:20:40 PM8/9/21
to CasADi
Dear Alex,

Check your square roots..

Best regards,
  Joris

Alessandro Maria Laspina

unread,
Aug 10, 2021, 10:17:24 AM8/10/21
to CasADi
How can I check the output of sqrt(uvel^2+v^2+w^2) for example? It says tho use sqrt(printme(..)) but I don't know where in the code this is supposed to go; before or after opti.sol()? It gives me this error: 
Error using casadi.MX/printme (line 837)
function MX_printme requires at least 2 arguments

Error in fun_cas_quat (line 315)
sqrt(printme(sqrt(uvel^2+v^2+w^2)))

Error in script_mpc_test_func (line 9)
[intg,dae]=fun_cas_quat(PAR);

Joris Gillis

unread,
Aug 10, 2021, 4:04:07 PM8/10/21
to CasADi
I see that the faq entry was lacking in information; now amended/
Please let me know if it is clearer now..

Best regards,
  Joris

Alessandro Maria Laspina

unread,
Aug 13, 2021, 5:03:07 AM8/13/21
to CasADi
I'm still a little puzzled as to what I am looking for. I did a printme on the value and it returns several NaN lines with a few 0's here and there. I don't understand what is breaking this constraint. Furthermore, I rewrote the ode's such that only quaternion's are used in coordinate transformations and I still get the same exact error.... 

I am not so confident that the problem is as straightforward as checking that there isn't a value in the equations of motion that causes singularities. I have done an experiment with all of my 9 control variables u, by varying them from their maximum value to the minimum value, and the resulting jacobian evaluations always given a real, finite, number. 
Reply all
Reply to author
Forward
0 new messages