## In ODE solver how to use the time step of each iteration within the differential equation?

Asked by Indu Muthancheri

### Indu Muthancheri (view profile)

on 14 Jun 2018
Latest activity Commented on by James Tursa

### James Tursa (view profile)

on 14 Jun 2018
One of my differential equation contains a term deltat. How do I access the time step of every ode solver iteration within the differential equation function.
[t,y]=ode45(@xx,tspan,yo);
%%in function xx
function dydt=xx(t,y)
dydt=zeros(3,1);
dydt(1)=A*deltat; %deltat is the time step during that iteration
dydt(2)=f(y(1),y(2),....)
dydt(3)=f(y(1),y(2)....)
end
Thank you so much in advance for the help!

James Tursa

### James Tursa (view profile)

on 14 Jun 2018
Your differential equation may contain something like "dy/dt" etc when written out, but that dt is NOT the time step used by the numerical integrator! This is simply derivative notation. What is the DE you are trying to solve?
Indu Muthancheri

### Indu Muthancheri (view profile)

on 14 Jun 2018
I am trying for mass balance, which requires the amount of gas in the chamber at that interval of time. So that is (flowrate_of_gas x deltat). The flow rate is fixed, so this is deltat not a differential equation.

R2017b

### Jan (view profile)

on 14 Jun 2018

deltat is the time step during that iteration
This is not a meaningful statement. Remember that ODE45 evaluates the equation several times for each step. Afterwards it uses an error control and rejects a step, when the tolerances are exceeded. It is impossible, that the mathematical notation will or can consider this. So please explain exactly, which mathematical problem you want to solve.

Indu Muthancheri

### Indu Muthancheri (view profile)

on 14 Jun 2018
Does this make it more of a description ?. Thank you.
Jan

### Jan (view profile)

on 14 Jun 2018
If delta_t would depend on the step size of the integrator, the derivative dX_a/dt would be greater, if the integrator reduces the step size. This is not physically meaningful. The step size chosen by the integrator depends on the tolerances, but the actual speeds cannot be affected by this apart from rounding errors.
Indu Muthancheri

### Indu Muthancheri (view profile)

on 14 Jun 2018
That makes sense! So instead of that I will give a constant value for GxdeltaT which is the total mass of air in the chamber at any instance if pressure inside the chamber is maintained. Thank you!!

Answer by James Tursa

### James Tursa (view profile)

on 14 Jun 2018

Based on your description, I would conclude that the deltat in the equation is simply the total amount of time that the mass flow rate has been taking place. If you start the simulation at time 0 and pass in t to your derivative routine, then you can simply use t in place of deltat in your MATLAB code.

Show 1 older comment
James Tursa

### James Tursa (view profile)

on 14 Jun 2018
If G*deltat is the "total air mass" and G is the mass flow rate, then it still sounds to me like the deltat is simply the total time that the mass flow rate has been taking place. I.e., simply use t instead of deltat in your code.
Indu Muthancheri

### Indu Muthancheri (view profile)

on 14 Jun 2018
if I use t then the differential equation will be a function of t as well. Is that what you are saying ?
James Tursa

### James Tursa (view profile)

on 14 Jun 2018
Yes, that's what it looks like to me since that is what is needed to calculate the total air mass at time t.