ODE solvers - passing parameters

102 ビュー (過去 30 日間)
Daz
Daz 2020 年 9 月 20 日
コメント済み: Star Strider 2020 年 9 月 22 日
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.

採用された回答

Star Strider
Star Strider 2020 年 9 月 20 日
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 件のコメント
Walter Roberson
Walter Roberson 2020 年 9 月 22 日
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 2020 年 9 月 22 日
Walter — Thank you.

サインインしてコメントする。

その他の回答 (1 件)

Stephan
Stephan 2020 年 9 月 20 日
編集済み: Stephan 2020 年 9 月 20 日
  1 件のコメント
Daz
Daz 2020 年 9 月 20 日
Thank you.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by