ODE solvers - passing parameters

105 次查看(过去 30 天)
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

请先登录,再进行评论。

更多回答(1 个)

Stephan
Stephan 2020-9-20
编辑:Stephan 2020-9-20

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by