2
votes

I have the following Matlab ODE code:

[t,y,~,~,ie] = ode23tb(@(t,y) RHSODE(t,y),[0,t_end], [i0;v0],options);

I want the ODE solver could also give me the result z, which is a function of y and dy/dt, such that z = f(y,dy/dt).

Does anyone know how to add such z into the output of the solver?

1

1 Answers

4
votes

There are two ways to do this. The most common, and usually fastest, way is to take advantage of your integration function (RHSODE in your case) and evaluate your function f after performing the integration. You haven't provided many details with your code but it might look something like this:

ydot = RHSODE(t,y);
z = f(y,ydot);

where t and y are the outputs from ode23tb. This requires that both RHSODE and f be vectorized (or you can wrap the above in a for loop).

The other way requires that you create an additional equation (or equations if z is a vector) inside of your integration function, RHSODE. Normally ode23tb integrates anything in this function so f must be multiplied by a factor of t to cancel this out. Again, your code might look something like this:

function ydot = RHSODE(t,y)
ydot0 = ... % Your original ODE(s)
z = f(y,ydot);
ydot = [ydot0;z*t]; % Make column vector