How to use root() and vpa() from symbolic math toolbox in simulink?

Illustration
Wouter - 2021-10-04T13:54:06+00:00
Question: How to use root() and vpa() from symbolic math toolbox in simulink?

I have computed an analytical solution to a problem, using the symbolic math toolbox I can now solve the problem with the following MATLAB function   function y = symbolicFunction(x0,x1,x2,x3,x4,x5) syms z y = vpa(root(z^6 - z^4*(- 300*x2*x5 + 450*x2^2 + 450*x5^2) - z^3*(7200*x2*x1 + 4800*x2*x4 - 4800*x5*x1 - 7200*x5*x4) - z^2*(18000*x2*x0 - 18000*x2*x3 - 18000*x5*x0 + 18000*x5*x3 + 50400*x1*x4 + 28800*x1^2 + 28800*x4^2) - z*(144000*x1*x0 - 144000*x1*x3 + 144000*x4*x0 - 144000*x4*x3) + 360000*x0*x3 - 180000*x3^2 - 180000*x0^2, z, 1)); end I want to implement this solution in a simulink model, but I cannot directly add this function in simulink. The most important error states Function 'syms' not supported for code generation. I have tried: Trying to write the analytical solution such that it doesn't require the symbolic toolbox, but I didn't manage to achive this. Compiling the function to C-code, but syms, root(), and vpa() are not supported for code generation. Is there any way to get this function or an equivalent to work in simulink?

Expert Answer

Profile picture of Prashant Kumar Prashant Kumar answered . 2025-11-20

I have solved this issue by using coder.extrinsic, in essence I have the following function in simulink
 
function output  = symbolicFunctionSimulink(x0,x1,x2,x3,x4,x5)
coder.extrinsic('symbolicFunction');
output = 0; % initializing output
output = symbolicFunction(x0,x1,x2,x3,x4,x5);
end

and had to alter my symbolicFunction.m such that simulink recognized it to be numeric

function y  = symbolicFunction(x0,x1,x2,x3,x4,x5)
syms z
y = double(vpa(root(z^6 - z^4*(- 300*x2*x5 + 450*x2^2 + 450*x5^2) - z^3*(7200*x2*x1 + 4800*x2*x4 - 4800*x5*x1 - 7200*x5*x4) - z^2*(18000*x2*x0 - 18000*x2*x3 - 18000*x5*x0 + 18000*x5*x3 + 50400*x1*x4 + 28800*x1^2 + 28800*x4^2) - z*(144000*x1*x0 - 144000*x1*x3 + 144000*x4*x0 - 144000*x4*x3) + 360000*x0*x3 - 180000*x3^2 - 180000*x0^2, z, 1)));
end

 


Not satisfied with the answer ?? ASK NOW

Get a Free Consultation or a Sample Assignment Review!