# How to use symbolic variables and functions (Syms) in a Simulink Matlab Function?

62 visualizaciones (últimos 30 días)
Rakesh el 14 de Sept. de 2014
Editada: Ali Sh el 22 de Jul. de 2021
I would like to create a Symbolic function within a Simulink Matlab Function to solve the variables h and t1. Matlab produces error "The function 'syms' is not supported for standalone code generation. See the documentation for coder.extrinsic to learn how you can use this function in simulation." when I try to compile the Simulink Matlab function with the following code.
syms Eq1(h,t1);
Eq1(h,t1) = h*t1;
I tried adding "coder.extrinsic('syms')" at the top, as shown below, and this generated the error "Undefined function or variable 'h'."
coder.extrinsic('syms');
syms Eq1(h,t1);
Eq1(h,t1) = h*t1;
How do I use symbolic variables and functions (Syms) in a Simulink Matlab Function?
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Denis Gurchenkov el 26 de Sept. de 2014
I think the only solution is to use the syms function inside a separate .m file and call that file as extrinsic.
Create a new .m file, e.g. solveSyms.m, put all your code there:
function out = solveSyms(in) % add necessary inputs and outputs
syms x y
f = x^2*y + 5*x*sqrt(y);
out = subs(f, x, in);
end
Now call this new file from the MATLAB Function block:
coder.extrinsic('solveSyms');
out = 0;
out = solveSyms(in);
I'm not very familiar with Symbolic Math toolbox, so you would need to correct the code above to use your equation. Also, read the doc for coder.extrinsic:
##### 6 comentariosMostrar 4 comentarios más antiguosOcultar 4 comentarios más antiguos
Emre Deniz el 1 de Ag. de 2018
I have the same problem, 'not numeric'...
Ali Sh el 22 de Jul. de 2021
Editada: Ali Sh el 22 de Jul. de 2021
In order to use the output of separate .m file in Simulink function, you have to change the type of function output to double. then, it'll work.
function out = solveSyms(in) % add necessary inputs and outputs
syms x y
f = x^2*y + 5*x*sqrt(y);
out = subs(f, x, in);
out = double(out);
end

Iniciar sesión para comentar.

### Más respuestas (2)

Andrew Reibold el 26 de Sept. de 2014
Editada: Andrew Reibold el 26 de Sept. de 2014
Example equation: abs(x+1) + abs(5x-1) = 6
How to solve (Here, I look for real solutions specifically):
syms x real;
solve(abs(x+1)+abs(5*x-2)==6,x);
ans =
7/6
-3/4
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Walter Roberson el 30 de Nov. de 2016
Possibly
coder.extrinsic('sym');
coder.extrinsic('symfun');
h = sym('h');
t1 = sym('t1')
EQ1 = symfun(h*t1, h, t1) ;
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Walter Roberson el 7 de Sept. de 2017
Note that any of these solutions can only work for "Normal" mode or for "Acceleration" mode, and cannot be used for Rapid Acceleration mode or for code generation to target. No part of the symbolic toolbox can have code generated for it. The use of coder.extrinsic and similar gives a work-around only for Acceleration mode, as there is still a backing MATLAB for that mode.

Iniciar sesión para comentar.

### Categorías

Más información sobre Code Performance en Help Center y File Exchange.

### Community Treasure Hunt

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

Start Hunting!

Translated by