1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

[Python] Solving Matlab ODE equation using python library Scipy in MatLab API

Discussão em 'Python' iniciado por Stack, Setembro 13, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to solve a stiff ODE system with other methods rather than Rosenbrock as ode23s from Matlab. I would like to use scipy library with various methods such as LSODA or Sundials.

    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 pyfunoption 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?

    Continue reading...

Compartilhe esta Página