Read time variable parameter values in an ode-solver loop

I have a function "solver" with a ode-solver that solves the differential equations of the function "funktion" for defined time steps. In the function "funktion" there are also the "parameters" Qc, Tc and Fc. These parameters are read from an excel file at the beginning. For every time step there is another value for these three parameters. So I want that the ode solver uses for every time step the accompanying parameter value. I tried to write in the function "funktion" that matlab has to use the value of "parameters" of the excel line t, but it doesn´t work.
What can I change to solve my problem?
Below I wrote the main important matlab codes.
feed.xlsx:
t Qc Fc Tc (this line is not in the excel file)
0 200 12 12
0,5 0 0 0
1 200 12 12
parameters = xlsread('feed.xlsx');
function [cumgas comp frac] = solver(parameters)
tspan = [0:(1/48):23];
options = odeset('RelTol',1e-3,'AbsTol',1e-6);
[t,X] = ode15s(@funktion,tspan,Xo,options,parameters);
function [adm_dt] = funktion(t,fractions,parameters)
Qc = parameters(t,1);
Tc = parameters(t,2);
Fc = parameters(t,3);
dSI = + (fSI_XC) * (kdis*Xc);
dSI = dSI + Qc * 0.003;

1 comentario

Now I tried the following code:
parameters = xlsread('feed.xlsx');
[cumgas comp frac] = solver(parameters);
_
function [cumgas comp frac] = solver(parameters)
tspan = [0:1/48:23];
for z = 1:length(parameters(1,:)) %length of parameters = length of tspan
options = odeset('RelTol',1e-3,'AbsTol',1e-6);
[t,X] = ode15s(@funktion,tspan,Xo,options,parameters(z,:);
end
-
function [adm_dt] = funktion(t,fractions,parameters)
Qc = parameters(1);
Tc = parameters(2);
Fc = parameters(3);
This loop works now. But I think that matlab calculates now for every z-value the X value at EVERY t step. But I want to calculate only one X value at the accompanying time step.
For example:
at t = 1 there is Qc(1) = 200, Tc(1) = 12 and Fc(1) = 12
at t = 2 there is Qc(2) = 0, Tc(2) = 0 and Fc(2) = 0.
But at the moment for Qc(1) = 200, Tc(1) = 12 and Fc(1) = 12 all time steps from 0 to 23 in 1/48-steps are calculated.
How can I change it?

Iniciar sesión para comentar.

 Respuesta aceptada

You shouldn't use this approach.
Better do this:
parameters = xlsread('feed.xlsx');
[cumgas comp frac] = solver(parameters);
_
function [cumgas comp frac] = solver(parameters)
tspan = 0:1/48:23;
options = odeset('RelTol',1e-3,'AbsTol',1e-6);
[t,X] = ode15s(@(t,y)funktion(t,y,parameters),tspan,Xo,options);
-
function adm_dt = funktion(t,fractions,parameters)
Qc = interp1(parameters(:,1),parameters(:,2),t);
Tc = interp1(parameters(:,1),parameters(:,3),t);
Fc = interp1(parameters(:,1),parameters(:,4),t);
Here I assumed that "parameters" is an nx4 matrix where column 1 is time, column 2 is Qc, column 3 is Tc and column 4 is Fc.
Best wishes
Torsten.

2 comentarios

Lena
Lena el 5 de Abr. de 2018
Thank you very very much! =)
Your code has functioned immediately. And it makes exactly what I wanted.
Best regards
Lena

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 5 de Abr. de 2018

Comentada:

el 5 de Abr. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by