필터 지우기
필터 지우기

ODE solvers - passing parameters

조회 수: 110 (최근 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일

카테고리

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