How to get only one or two output times from ODE solver?
Show older comments
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?
Accepted Answer
More Answers (0)
Categories
Find more on Ordinary Differential Equations 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!