How to use syms inside a function that is called in simulink?

2 views (last 30 days)
I have an Extended Kalman Filter in which I have written Matlab functions for jacobians.
I have used syms from symbolic toolbox in order to compute the jacobians. However I get the error message "Function 'syms' not supported for code generation." when I run the Simulink Function.
How do I avoid this error and compute the jacobians ?
The following is my jacobian function which I use in the EKF block.
function Jss = stateTransitionJacobian(X,U)
syms x [3 1];
syms y [3 1];
syms z [4 1];
syms a [3 1];
syms b [3 1];
ge=[0; 0; 9.81];
Qplus=[z1 -z2 -z3 -z4; z2 z1 -z4 z3; z3 z4 z1 -z2; z4 -z3 z2 z1];
Qminus=[z1 -z2 -z3 -z4; z2 z1 z4 -z3; z3 -z4 z1 z2; z4 z3 -z2 z1];
I34=[0 1 0 0; 0 0 1 0; 0 0 0 1];
z1=0.5*Qplus*([b; 0]);
% Xdot=[p1; v1; q1];
Js=jacobian([x; y; z]+[x1; y1; z1]*deltat,[x; y; z]);
Jss=subs(Js,[x; y; z; a; b],[p; v; q; fm; omegam]);

Accepted Answer

Divya Yerraguntla
Divya Yerraguntla on 30 Mar 2020
Hi Vinayak,
As the error describes the syms function is not supported for Code Generation. So a possible workaround in your case would be to use matlabFunction function in order to convert your symbolic expressions to function handle. This eliminates the dependency on Symbolic Math Toolbox and hence facilitates Code Generation.
Workflow to use matlabFunction:
  1. Create your formula in Symbolic Math Toolbox.
  2. Use matlabFunction to create a MATLAB function that do not depend on Symbolic Math Toolbox anymore.
  3. Use MATLAB Deployment to reach your goal.
Have a look at matlabFunction for examples on function usage.
Hope it helps!

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!