ODE solvers - passing parameters
26 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Star Strider
on 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 Comments
Walter Roberson
on 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
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!