ODE with variables dependent on the previous step of integration

19 visualizaciones (últimos 30 días)
Marvin Gierling
Marvin Gierling el 17 de Nov. de 2021
Comentada: Bjorn Gustavsson el 17 de Nov. de 2021
Hello!
I'm using the ode15s function to solve a system of stiff equations.
I need to run the code a couple hundred times to produce a dataset for a neural network in another project.
Problem: The function inside the ODE needs to use variables, that depend on the previous result of the ODE. My code is working, but it is far to slow to use it to create a big set of data. I can't interpolate the changing variabels beforhand.
My solution: I'm starting the ode solver with the first set of variables. The last step of the function inside the ode calls another function to calculate the next set of variables.
My code:
(Please note, that the real code is way more complicated an uses matrices as inputs etc. I just tried to give an idear of the structure of the programm.)
% Main Programm
global a b c
% Defining the first set of variables (Example)
a = 1
b = 2
c = 3
% Defining the steps for the integration
tspan=[0:tstep:tfin];
% Calling the ODE
[t,u] = ode15s('subroutine',tspan,u0);
end
For the under programm
% Under Programm
function u = ode15s('subroutine',tspan,u0);
global a b c
% Function (Example)
u = 1*a + 2*b + 3*c
% Call function for new variables
[a,b,c] = funtion_for_variables(u);
end
For the function for the variables
function [a,b,c] = funtion_for_variables(u)
% Calculation for new variables
a = u*a
b = 2*u*b
c = 3*u*c
end
My thoughts regarding the problem: When im using the programm as described, the "subroutine" and thus the "funtion_for_variables" is called nearly a Million times. If I dont use the "funtion_for_variables" and just set the variables as fixed, it's just called 10000 times.
So I think because I'm changing the varaibles the ODE has problems to reach the needed accuracy.
My approch: I already looked into using different ODEs, no noticable change. I tried the different odest options to reduce the needed accuracy, no change. I rounded the variables in the subroutine, so the changes of the variables would not be significant, no change. I tried to optimize the code wherever possible, but the main problem remains the sheer number of calls, I think.
Additional information: The real programm is used to simulate a chemical reaction inside an fixed bed reactor with finite elements. The changing varaibels are material properties like densety or heat capacity.
Thanks in advance and kind regards
Marvin
  2 comentarios
Star Strider
Star Strider el 17 de Nov. de 2021
My suggestion is to not use global variables, and instead pass them as extra parameters. The ‘subroutine’ function then becomes —
function [t,u] = subroutine(t,u,a,b,c)
and the call to it becomes —
[t,u] = ode15s(@(t,u)subroutine(t,u,a,b,c),tspan,u0);
with the extra parameters already defined in the workspace as the output of the ‘funtion_for_variables’ call.
I am not certain that this will make the code faster (although it could), however it will make it more efficient and less prone to errors.
.
Bjorn Gustavsson
Bjorn Gustavsson el 17 de Nov. de 2021
You should definitely remove the global-variable as a means to hold information about the state of your physical system - the ODE-solvers do not step forward in a straightforward manner such that the variables assigned to those globals will be "correct at the previous time-step". If you have a more complicated physical system where other parameters also vary according to some differential equations (temperature/energy-equation etc) you might have to include them in your system of ODEs to model the entire system. If you have something that really depends on the previous step of integration you might have a "delay differential equation" - there are also solvers for such as dde23.

Iniciar sesión para comentar.

Respuestas (0)

Categorías

Más información sobre Ordinary Differential Equations en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by