MX function free variable

704 views
Skip to first unread message

Mosc1

unread,
Nov 19, 2020, 3:47:32 AM11/19/20
to CasADi
Hello,
I am getting this error in matlab,

my matlab code is:

q0 = MX.sym('q0');
qx = MX.sym('qx');
qy = MX.sym('qy');
qz = MX.sym('qz');
q = [q0, qx, qy, qz]';

R_I2B = MX.sym('R_I2B',3,3);
R_I2B(1,1) = 2*(q0^2+qx^2)-1;
R_I2B(1,2) = 2*(qx*qy+q0*qz);
R_I2B(1,3) = 2*(qx*qz-q0*qy);
R_I2B(2,1) = 2*(qx*qy-q0*qz);
R_I2B(2,2) = 2*(q0^2+qy^2)-1;
R_I2B(2,3) = 2*(qy*qz+q0*qx);
R_I2B(3,1) = 2*(qx*qz+q0*qy);
R_I2B(3,2) = 2*(qy*qz-q0*qx);
R_I2B(3,3) = 2*(q0^2+qz^2)-1;

rotational_matrix_I2B = Function('rotational_matrix_I2B',{q},{R_I2B});
aa = rotational_matrix_I2B([0, 0, 0, 1]); <-- the error appears here

and the error is: 

Error using casadi.Function/call (line 942)
.../casadi/core/mx_function.cpp:404: Cannot evaluate "rotational_matrix_I2B:(i0[4])->(o0[3x3]) MXFunction" since variables [R_I2B]
are free.

Error in casadi.Function/paren (line 1770)
        res = self.call(varargin);

Error in casadi.Function/subsref (line 1751)
        [varargout{1:nargout}]= paren(self, s.subs{:});

Error in uav (line 86)
aa = rotational_matrix_I2B(ctr.x0(7:10));

Anyone know how I can fix it?

Best Regards


Joris Gillis

unread,
Nov 19, 2020, 4:33:00 AM11/19/20
to CasADi
The expression R_I2B you end up with after all the assignments is still dependant on the symbol R_I2B.
You cannot evaluate Functions that omit some symbols from the list of inputs.

Use MX.zeros to initialze R_I2B, not a symbol.

  Joris

Mosc1

unread,
Nov 19, 2020, 6:50:06 AM11/19/20
to CasADi
You are right, my fault. Many thanks =D

Mosc1

unread,
Nov 19, 2020, 6:53:19 AM11/19/20
to CasADi
I have tested the same code with the only difference that insted to use MX variable I have used SX, and It works without the initializzation of R_I2B to zero. Could you explain why ?
Many thanks for your support.

Joris Gillis

unread,
Nov 19, 2020, 6:59:11 AM11/19/20
to CasADi
SX comes with more advancee simplication logic,
such that the dependence on the stray symbol is dropped.

This is bad style regardless, don't create symbols if you don't need them..
  Joris

Mosc1

unread,
Nov 19, 2020, 7:06:11 AM11/19/20
to CasADi
Many thanks for your advice. I will apply them. 

yingjie jiang

unread,
Feb 9, 2022, 8:46:15 AM2/9/22
to CasADi
Thank you for you explanation. I'm new to CasADi, could you please tell me why R_I2B is still dependant on R_I2B after the assignment? Isn't it already defined by q? Besides, I don't understand how R_I2B can depend on itself, what does this mean?
Thanks!

Reply all
Reply to author
Forward
Message has been deleted
0 new messages