ODE solvers - passing parameters

104 vues (au cours des 30 derniers jours)
Daz
Daz le 20 Sep 2020
Commenté : Star Strider le 22 Sep 2020
trange = [0 120];
Cin = [5.1 3.1 387.05];
%Q=0;
[t,c] = ode45(@simul_dif, trange, Cin);
My function is,
function f = simul_dif(t,c)
k1 = 3.58*10^8;
k2 = 2.08*10^7;
E1DR = 9758.3;
E2DR = 9758.3;
dHr = 78;
A = 0.5;
h = 1;
Q=100;
k11 = (k1*exp(-E1DR/c(3)));
k21 = (k2*exp(-E2DR/c(3)));
rho = 934.2;
Cp = 2;
f(1,1) = ((-dHr/(rho*Cp)) * ((k11*c(1))-(k21*c(2))))+(Q/(A*h*rho*Cp));
f(2,1) = (-k11*c(1))+(k21*c(2));
f(3,1) = (k11*c(1))-(k21*c(2));
end
What is need to know that how to send a contant Q value from command window to the function instead of define it in the function.

Réponse acceptée

Star Strider
Star Strider le 20 Sep 2020
It is actually a bit more involved than simply passing parameters when you are using the function with any of the ODE solvers (ode45 specifically here).
The function declaration must be:
function f = simul_dif(t,c,Q)
and the call to it in ode45 must be:
[t,c] = ode45(@(t,c)simul_dif(t,c,Q), trange, Cin);
With both of those changes, it will work.
There are additional considerations if you are defining ‘Q’ as individual elements of a vector in a loop.
  3 commentaires
Walter Roberson
Walter Roberson le 22 Sep 2020
I am not getting any NaN when I make the obvious changes to your code.
trange = [0 120];
Cin = [5.1 3.1 387.05];
Q = 100;
[t,c] = ode45(@(t,c)simul_dif(t,c,Q), trange, Cin);
function f = simul_dif(t,c,Q)
k1 = 3.58*10^8;
k2 = 2.08*10^7;
E1DR = 9758.3;
E2DR = 9758.3;
dHr = 78;
A = 0.5;
h = 1;
k11 = (k1*exp(-E1DR/c(3)));
k21 = (k2*exp(-E2DR/c(3)));
rho = 934.2;
Cp = 2;
f(1,1) = ((-dHr/(rho*Cp)) * ((k11*c(1))-(k21*c(2))))+(Q/(A*h*rho*Cp));
f(2,1) = (-k11*c(1))+(k21*c(2));
f(3,1) = (k11*c(1))-(k21*c(2));
end
Star Strider
Star Strider le 22 Sep 2020
Walter — Thank you.

Connectez-vous pour commenter.

Plus de réponses (1)

Stephan
Stephan le 20 Sep 2020
Modifié(e) : Stephan le 20 Sep 2020
  1 commentaire
Daz
Daz le 20 Sep 2020
Thank you.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by