How to get only one or two output times from ODE solver?
6 次查看(过去 30 天)
显示 更早的评论
I am programming a nonlinear optimization, during which an ODE is solved multiple times at each objective function call. So I would like to make this part as efficient as possible. What I have noticed is that it seems to be impossible to get only one, or only two values in time as output from the solver.
Take this ODE for example:
function dx = CstrConsecutiveReaction(t, x, params)
k_1 = params(1);
k_2 = params(2);
k_3 = params(3);
k_4 = params(4);
q_in = params(5);
V_liq = params(6);
A_in = params(7);
B_in = params(8);
C_in = params(9);
D_in = params(10);
A = x(1);
B = x(2);
C = x(3);
D = x(4);
% RHS of ODE system
dx(1,1) = -k_1*A + q_in/V_liq*(A_in - A);
dx(2,1) = 2*k_1*A - 2*k_2*B*B + 2*k_3*C - k_4*B + q_in/V_liq*(B_in - B);
dx(3,1) = k_2*B*B - k_3*C + q_in/V_liq*(C_in - C);
dx(4,1) = k_4*B + q_in/V_liq*(D_in - D);
end
If I call it like this:
x0 = [1.5, 0.1, 0, 0];
params = [1.0, 1.5, 0.75, 0.15, 3, 15, 0.5, 0, 0, 0];
[t_sol, x_sol] = ode15s(@(t, x) CstrConsecutiveReaction(t, x, params), 0:1, x0)
I get 33x4 values! Why? If I use 0:0.5:1 as time vector, I get three.
I am only interested in the value at t=1, so ideally, I would like to get a 1x4 output from ode15s. Right now, I need to filter the values I need after each solver call, like this:
A = x_sol(end, 1);
D = x_sol(end, 4);
Any suggestions how I could improve this?
0 个评论
采纳的回答
Torsten
2019-2-21
编辑:Torsten
2019-2-21
If you use two elements in tspan, ode15s returns the solution at intermediate output times chosen from its internal stepsize control. If you use more than two elements in tspan, ode15s returns the solution at these specified times. So, x_sol will at least be a 3x4 matrix.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!