Errors in the MATLAB function added to simulink
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Following is my code for the MATLAB function block in the Simulink.
function gf = fcn(u)
syms x;
y = 0.13*x^4 - 0.9308*x^3 + 2.2267*x^2 + 0.2751*x + 0.0503;
g = finverse(y);
f = 2.19*u;
gf = compose(g,f);
end
I am getting a lot of errors as follows and I do not know how to correct them. I have read the available documentation on the MATLAB function.
Function 'syms' not supported for code generation.
Undefined function or variable 'x'.
Undefined function or variable 'y'. The first assignment to a local variable determines its class.
Undefined function or variable 'g'. The first assignment to a local variable determines its class.
Please help.
Thanks!
0 commentaires
Réponses (1)
Walter Roberson
le 4 Sep 2019
Modifié(e) : Walter Roberson
le 4 Sep 2019
Simulink operates in several different modes. In one of the modes, optimization is turned off and everything is simulated at the MATLAB level. That one mode can use the Symbolic Toolbox operations without change if I recall correctly. But it is not the default mode.
The default mode does some optimization producing internal data structures that are faster to simulate but with the MATLAB runtime hanging around in the background. That mode requires that all symbolic toolbox functions be explicitly coded using coder.extrinsic(). You also need to replace the syms x call with x=sym('x') because this mode does not support assignin() that syms uses to create the variable.
Simulink also can operate in a mode where it generates C code and compiles it for faster execution. In this mode, the matlab execution engine is not present and you cannot use the symbolic toolbox at all.
Finally Simulink can also generate code for use on embedded systems or FPGA or raspberry pi or Arduino; the symbolic toolbox is unavailable and most of the targets do not even have any operating system.
So what should you do? Well, you should recode so that you do not need to use the symbolic toolbox in Simulink.
Take your existing fcn and pass it sym('u') at the MATLAB level. You will get out a symbolic expression that can replace the symbolic work you do there. You can then use matlabFunction() with the 'file' option to generate matlab code. Take the generated code and touch it up to do preallocation that Simulink wants and use the resulting pure matlab code instead of the symbolic toolbox calls
3 commentaires
Walter Roberson
le 4 Sep 2019
If you do
syms u;
syms x;
y = 0.13*x^4 - 0.9308*x^3 + 2.2267*x^2 + 0.2751*x + 0.0503;
g = finverse(y);
f = 2.19*u;
gf = compose(g,f);
simplify(gf)
then you will get
(39^(1/2)*(272498*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(1/3)*(136249*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(1/3) - 8541000000*u + 25*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(2/3) + 14512125325)^(1/2) - 14512125325*(136249*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(1/3) - 8541000000*u + 25*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(2/3) + 14512125325)^(1/2) - 25*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(2/3)*(136249*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(1/3) - 8541000000*u + 25*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(2/3) + 14512125325)^(1/2) + 8541000000*u*(136249*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(1/3) - 8541000000*u + 25*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(2/3) + 14512125325)^(1/2) + 171145008*39^(1/2)*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(1/2))^(1/2))/(3900*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(1/6)*(136249*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(1/3) - 8541000000*u + 25*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(2/3) + 14512125325)^(1/4)) - (39^(1/2)*(136249*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(1/3) - 8541000000*u + 25*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(2/3) + 14512125325)^(1/2))/(3900*(18*13^(1/2)*(9467117712000000000000*u^3 - 46405209395666241120000*u^2 + 63689131782957633368800*u - 1204782748056965920359)^(1/2) - 2792886501600*u + 13803164115967)^(1/6)) + 179/100
If you were to try to use that directly, then you have the problem that it is not vectorized. But
>> matlabFunction(ans)
ans =
function_handle with value:
@(u)sqrt(3.9e+1).*1.0./(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).^(1.0./6.0).*sqrt(u.*-8.541e+9+(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).^(1.0./3.0).*1.36249e+5+(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).^(2.0./3.0).*2.5e+1+1.4512125325e+10).*(-2.564102564102564e-4)+(sqrt(3.9e+1).*1.0./(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).^(1.0./6.0).*1.0./(u.*-8.541e+9+(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).^(1.0./3.0).*1.36249e+5+(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).^(2.0./3.0).*2.5e+1+1.4512125325e+10).^(1.0./4.0).*sqrt(sqrt(u.*-8.541e+9+(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).^(1.0./3.0).*1.36249e+5+(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).^(2.0./3.0).*2.5e+1+1.4512125325e+10).*-1.4512125325e+10+(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).^(1.0./3.0).*sqrt(u.*-8.541e+9+(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).^(1.0./3.0).*1.36249e+5+(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).^(2.0./3.0).*2.5e+1+1.4512125325e+10).*2.72498e+5-(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).^(2.0./3.0).*sqrt(u.*-8.541e+9+(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).^(1.0./3.0).*1.36249e+5+(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).^(2.0./3.0).*2.5e+1+1.4512125325e+10).*2.5e+1+u.*sqrt(u.*-8.541e+9+(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).^(1.0./3.0).*1.36249e+5+(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).^(2.0./3.0).*2.5e+1+1.4512125325e+10).*8.541e+9+sqrt(3.9e+1).*sqrt(u.*-2.7928865016e+12+sqrt(1.3e+1).*sqrt(u.*6.368913178295763e+22-u.^2.*4.640520939566624e+22+u.^3.*9.467117712e+21-1.204782748056966e+21).*1.8e+1+1.3803164115967e+13).*1.71145008e+8))./3.9e+3+1.79e+2./1.0e+2
gives you something that is vectorized. You can replace your fcn with the body of that expression (you might have to break it up into smaller pieces and do some pre-allocation, possibly)
Voir également
Catégories
En savoir plus sur Simulink Functions dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!