Borrar filtros
Borrar filtros

ODE - Save variables for every time step

12 visualizaciones (últimos 30 días)
sko
sko el 8 de Mzo. de 2021
Comentada: Bjorn Gustavsson el 22 de Abr. de 2021
Hello Guys,
I am stuck with saving time dependent variables inside a model function ... I was browing some already answered questions but none seems to help.
I have the following:
[t, f_val] = ode15s(@model,tspan,yeq);
That yields my f_val as a function of time.
But inside my model function I have also yy variable (time dependent) I would like to trace
function [f, yy] = model(t,yeq)
f= ... %some expression
yy= ... %some expression
How can I extract yy as a function of time?
Cordially

Respuestas (2)

Bjorn Gustavsson
Bjorn Gustavsson el 8 de Mzo. de 2021
This is something that is (simplest?) done by calculating the second output once after you've integrated the ODE-system. Either in one fell swoop if your ODE-function can handle arrays for input of t and f:
[f_second_time_around_or_tilde, yy] = model(t,f_val);
Or you can quickly loop over t:
for i_t = numel(t):-1:1,
yy(i_t,:) = model(t(i_t),f_val(i_t,:));
end
Since the odeNN-functions take cunningly adaptive steps in time we cannot trivially save all the values of yy along the steps...
HTH
  3 comentarios
Bjorn Gustavsson
Bjorn Gustavsson el 8 de Mzo. de 2021
What with the second loop-based suggestion? That explicitly calculates yy for every step in the solution you have...
sko
sko el 8 de Mzo. de 2021
Hello Bjorn,
I re thought my code a little bit and I think I understand my problem better, however, still without solution.
In my main code i need to 1) specify my initial conditions (initial concentrations):
%% Concentration in gas phase %%
l=1:1:ent.nz+1;
i=1:1:ent.ncg; % ncg
rx.Cg(i,l)=0;
rx.Cg(1,l)=1; %A
%% Concentration in solid phase %%
l=1:1:ent.nz+1;
k=1:1:ent.nr;
i=1:1:ent.ncg;
rx.particle.Cp(i,k,l)=0;
2) Load those concentrations in terms of single vector (for every l,k and i I create a single vector yeq)
3) Start ODE solver
tspan=[0 10];
[t, f_val] = ode15s(@model,tspan,yeq);
The challange with my model is the fact that time derivatives I need to specify depend on concentration in space (i.e. rx.dCgdt(i,l) and rx.particle.dCpdt(i,k,l) are functions of rx.Cg(i,l) and rx.particle.Cp(i,k,l) ) Thus, my
@model
function must first deload concentrations rx.Cg(i,l) and rx.particle.Cp(i,k,l) from loaded vector yeq, and secondly specify x.dCgdt(i,l) and rx.particle.dCpdt(i,k,l). Once time derivatives calculated as demanded by ode solver, I need to load them into single vector dydeq specified as f in the function model.
Finally,
[t, f_val] = ode15s(@model,tspan,yeq);
Yields my concentrations rx.Cg(i,l) and rx.particle.Cp(i,k,l) with time.

Iniciar sesión para comentar.


sko
sko el 22 de Abr. de 2021
Hello Guys,
I come once again on this general topic. I really really dont get how to code function so that I obtain multiple outputs. I will reprhase my question so that it becomes clearer.
I have an ODE solver that works just fine that I call as follows:
[time,concentration] = ode15s(@(t,yeq)model(t,yeq,dr,nrp,Deff),timespan,yeq);
c=concentration';
The function that calculates time derivatives dcdt is model function. However, inside this function I also want to calculate some others parameters regarding kinetics (not only concentrations I obtain from ODE solver) such as conversion, production selectivities that all depend on calculated concentration and are thus time dependent. Therefore, I need to correct my model function so that not only it provides me calculated concentrations but also those other parameters. Although I calculate those parameters in my model function, I dont seem to get how to extract them in my main file.
My model function is already structured so that I calculate time derivatives dcdt but also selectivity etc ...
Someone please help ....
  10 comentarios
sko
sko el 22 de Abr. de 2021
Found it!
for i_t = numel(time):-1:1,
[yy(i_t,:),PAR1(i_t,:),PAR2(i_t,:)] = model(time(i_t),concentration(i_t,:),dr,nrp,Deff);
end
Thank you very very much both of you! It was a mismatch in dimensions of PAR that model produced.
Bjorn Gustavsson
Bjorn Gustavsson el 22 de Abr. de 2021
Yes, the in solution I provided I couldn't now what the dimensions of your additional outputs were expected to be, therefore I opted to provide something that you could easily adjust to match the dimensions of your outputs (one or the other parameter might even have different dimensions at different time-steps as far as I could see...).

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by