How to initialize Sympy python function in Simulink
8 次查看(过去 30 天)
显示 更早的评论
Hello,
I want to solve ODE in Simulink with python function (derived in Sympy). This is just example, real Sympy equations will be larger.
Python function called Pyfun.py:
from sympy import lambdify, Matrix, symbols
t,x1,x2 = symbols('t x1 x2')
def create_symbolic_expression():
expr = Matrix([[x2], [(1-x1**2)*x2-x1]]);
return expr
def lambdify_expression(expr):
return(lambdify([t,x1,x2],expr, modules='numpy'))
Then I have MATLAB Function in Simulink that looks like this
function y = fcn(t,u)
y = [0;0];
coder.extrinsic('py.Pyfun.create_symbolic_expression')
coder.extrinsic('py.Pyfun.lambdify_expression')
expr = py.Pyfun.create_symbolic_expression();
myfunc = py.Pyfun.lambdify_expression(expr);
y = double(myfunc(t,u(1),u(2)));
Output of this function is integrated and going back as input, as it should. Everything here works well and gives right results.
My problem is, that the MATLAB Function is calling the "create" and "lambdify" symbolic expression every iteration and it makes the calculation very slow. The question is, if there is a way to initialize the python code before the Simulink simulation and only calculate with "myfunc" expression/function. I tried to call this in InitFcn in Callbacks like this
coder.extrinsic('py.Pyfun.create_symbolic_expression')
coder.extrinsic('py.Pyfun.lambdify_expression')
expr = py.Pyfun.create_symbolic_expression();
myfunc = py.Pyfun.lambdify_expression(expr);
and run MATLAB Function only like this
function y = fcn(t,u)
y = [0;0];
y = double(myfunc(t,u(1),u(2)));
but it says "Undefined function or variable 'myfunc'.". Thank you.
0 个评论
采纳的回答
Ayush Aniket
2024-2-19
Hi Ondrej,
To avoid the repeated initialization of your Python function within the Simulink model, you can use persistent variables in your MATLAB Function block to store the function handle across function calls. The persistent variable will be initialized only once when the model starts running. Refer to the documentation below to read more about it:
You can modify your MATLAB Function block as shown below:
function y = fcn(t,u)
y = [0;0];
coder.extrinsic('py.Pyfun.create_symbolic_expression')
coder.extrinsic('py.Pyfun.lambdify_expression')
persistent myfunc
if isempty(myfunc)
expr = py.Pyfun.create_symbolic_expression();
myfunc = py.Pyfun.lambdify_expression(expr);
end
y = double(myfunc(t,u(1),u(2)));
end
In the code above, 'myfunc' is declared as a persistent variable, which means that its value is retained in memory between calls to the function. The check 'isempty(myfunc)' ensures that the Python function is only created and lambdified once, when 'myfunc' is not yet initialized (i.e., during the first function call).
The InitFcn callback in the model's Callbacks is not suitable for initializing variables used within the MATLAB Function block because the scope of variables defined in the InitFcn is not shared with the MATLAB Function block.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Event Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!