MATLAB Answers

How to get only one or two output times from ODE solver?

10 views (last 30 days)
rotton on 21 Feb 2019
Commented: rotton on 25 Feb 2019
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);
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

Torsten on 21 Feb 2019
Edited: Torsten on 21 Feb 2019
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.
  1 Comment
rotton on 25 Feb 2019
Ok, so what I originally intended seems to be impossible.
I posted a follow-up question, which shows the context where getting a single value out of an ODE solver could be helpful, although I am not sure this would improve performance that much.

Sign in to comment.

More Answers (0)





Community Treasure Hunt

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

Start Hunting!