import a symbolic expression to simulink and plot it

I have a defined and used symbolic expression in MATLAB
syms y x
x(y) = y^5+2*y+y*(3+9*y^4);
I would like to import this expression to Simulink through a simin block to be plotted from 0 to 7 only on a scope. Can someone please help?
Thanks

 Accepted Answer

There is no possibility of doing that. Symbolic expressions are not valid Simulink signals.
If you matlabFunction() the expression and func2str() and strip off the @() part and string() the result, then you would end up with a string object that could be used as a signal.
Once you had the signal you would use a MATLAB Function Block and str2sym() and fplot() the resulting expression if the purpose is just plotting over a fixed window. If you wanted to plot over each value of an input signal then you would subs() the value into the symbolic expression and double() the results, and you would probably use animatedline for the plotting.
In order to get the symbolic part to work at all you will need to coder.extrinsic the symbolic functions. You will not be able to use acceleration at all. Deploying to hardware or real-time is out of the question.
If this all seems very round-about then Yes, it is. Simulink is not designed to work with symbolic expressions, and since the task is to do symbolic work in Simulink you have implicitly ruled out using matlabFunction at the MATLAB level to create an anonymous function and invoke that anonymous function in Simulink.
Note that you cannot just string() the symbolic expression at the MATLAB level and send that to Simulink. string() of a symbolic expression is not documented, but in practice it is string() of char() of the symbolic expression. char() of a symbolic expression is not documented, but in practice it produces an output that is not a matlab expression, and is not an internal symbolic engine expression (MuPAD language), and is not something that you can use str2sym to recover the symbolic expression. char() and string() of symbolic expressions should be understood as being strictly for display purposes and not as something that can be executed.
For example if you convert
syms x
string(piecewise(x>=-1&x<=2,1,0))
You get
"piecewise(x in Dom::Interval([-1], [2]), 1, symtrue, 0)"
Notice that the :: is not valid matlab input and not valid symbolic input. This is why you must go through matlabFunction(). Which will give you a hassle if you have int() or piecewise() or symsum....

More Answers (0)

Products

Release

R2022a

Community Treasure Hunt

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

Start Hunting!