Matlab ODE function solving using Python Scipy library.

23 次查看(过去 30 天)
I would like to solve a system of stiff ODE equations from Matlab with other methods from the scipy library such as LSODA or Sundials rather than Rosenbrock (ode23s).
A matlab ODE complex function defined as;
function [dpdt]=damper_rebo(t,P,amp,freq,pars)
.....
with amp, frequency constants and pars a cell of parameters with constants parameters.
It works fine with;
tic
[tsol,ysol]=ode23s(@(t,P) damper_rebo(t,P,amp,freq,pars),[0 0.5/freq],P_0,options);
toc
I am trying to compare with other methods to solve ODE with stiff equations like LSODA and BDF.
I loaded the python environment and installed all the path requirements, then I tried this:
py.importlib.import_module("scipy")
%--Call out the constant parameters from the function--%%
pars=params7();
pyrun("from scipy.integrate import LSODA")
%-----------------------------------------%
%---Excitation Signal Properties of the Damper---%
amp=25.25e-3; % Modal excitational amplitudes (m)
v_n=0.151;
freq=v_n/2/pi/amp; % Modal frequency (Hz)
%-------------------------------------%
%--Initial Pressure conditions--%
Pg_0=pars{68};
P_0=[0.01,0.001,1e5,2e5,Pg_0]; % At equilibrium three chambers at the same pressure
%-------------------------------%
% %-------------------------------%
%--Atmospheric Surrounding Temp and Pressure--%
T_atm=15; % Fluid Initial Properties by the supplier
p_atm=101325;
%---------------------------------------------%
Tspan = py.list([0.0, 0.5/freq]);
pyfun = py.str('lambda t,P: damper_rebo(t,P,amp,freq,pars)');
or
pyfun = py.function_handle(@(t,P) damper_rebo(t,P,amp,freq,pars));
sol=py.scipy.integrate.solve_ivp(pyfun,Tspan, py.numpy.array(P_0),pyargs(method="BDF", rtol=0.00001, atol=1e-06));
The first `pyfun`option brings an error that `
Python Error: TypeError: 'str' object is not callable`
The second option brings an error too with
`Unable to resolve the name 'py.function_handle'.`
1. How can I pass the Matlab ODE function to a callable python function?
2. How can I pass the extra arguments in a python manner?

回答(2 个)

Tejas
Tejas 2024-4-5
编辑:Tejas 2024-4-5
Hello Simon,
It appears from the code shared above, that you are attempting to pass a MATLAB function handle as an input argument to a Python function, which has led to the two issues mentioned.
In the first issue, ‘py.str’ converts the function call into a string, which subsequently renders it uncallable from any function. The second issue arises due to an attempt to pass a MATLAB function handle as an input argument to a Python function, a process for which there currently isn't a direct method. For further understanding of what is supported and what isn't when interacting with Python from MATLAB, please refer to the following documentation: https://www.mathworks.com/help/releases/R2022b/matlab/matlab_external/passing-data-to-python.html .
This documentation also includes details on how MATLAB variables are interpreted as Python variables, offering guidance on how to pass input arguments when calling a Python function from MATLAB.
To address the above two issues, I suggest the following workaround:
  • Create a Python function that calls the ‘damper_rebo’ function using the MATLAB Engine API.
  • Pass this Python function as an input argument to the ‘solve_ivp’ function.
For more information on how to use the MATLAB Engine API, please consult the following two pieces of documentation:
Hope it helps!
  1 个评论
Simon Mwakitabu
Simon Mwakitabu 2024-4-14
I had couple of conversations with the Mathworks team, we tried to get a solution. Function handling is not an easy task by the way.

请先登录,再进行评论。


Mike Croucher
Mike Croucher 2024-4-17
  1 个评论
Simon Mwakitabu
Simon Mwakitabu 2024-4-22
I am using MAtlab R2023B version. It seems to not have the solver installed. I would wait for my licence to be updated for the R2024 to synchronise.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Call Python from MATLAB 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by