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

조회 수: 5 (최근 30일)
winkmal
winkmal 2019년 2월 21일
댓글: winkmal 2019년 2월 25일
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?

채택된 답변

Torsten
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.
  댓글 수: 1
winkmal
winkmal 2019년 2월 25일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by