# Accessing current time-point inside Differential Equation function while solving

**URL:** <https://discourse.julialang.org/t/accessing-current-time-point-inside-differential-equation-function-while-solving/124876>\
**Category:** New to Julia\
**Tags:** question, differentialequation\
**Created:** [January 17, 2025, 2:25pm UTC](https://discourse.julialang.org/t/accessing-current-time-point-inside-differential-equation-function-while-solving/124876 "2025-01-17T14:25:58Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [January 17, 2025, 2:25pm UTC](https://discourse.julialang.org/t/accessing-current-time-point-inside-differential-equation-function-while-solving/124876/1 "2025-01-17T14:25:58Z")

</div>

Hello everyone,

In function shown below I have a vector named vf that I want to access at some specific timepoint in my function `tmp!()`.  
so something like this -

```julia
 time vf access
   1. 0.001
   2. 0.002
   3. 0.003

```

and so on

I was just wondering if there is already a way to do this because when i tried to access `vf` as `vf[t]` and this doesn’t work as while solving it doesn’t give exact `t` values to be used as index.

I have following function:

```julia
function tmp!(du, u, p, t)
    
    i, j = p
    quack, = u

    vf = [0.001, 0.002, 0.003, 0.004]

    du[1] = vf[t] * i *quack

end

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 17, 2025, 2:30pm UTC](https://discourse.julialang.org/t/accessing-current-time-point-inside-differential-equation-function-while-solving/124876/2 "2025-01-17T14:30:13Z")

</div>

what do you want this to do at non t=0.5?

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [January 17, 2025, 2:30pm UTC](https://discourse.julialang.org/t/accessing-current-time-point-inside-differential-equation-function-while-solving/124876/3 "2025-01-17T14:30:46Z")

</div>

for this example lets say dt is 1.

Or maybe I didnt uderstand what you meant.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [January 17, 2025, 2:35pm UTC](https://discourse.julialang.org/t/accessing-current-time-point-inside-differential-equation-function-while-solving/124876/4 "2025-01-17T14:35:25Z")

</div>

ODEs are continuous in time. If the function isn’t defined at all time points (within your tspan) you don’t have an ODE. Even if `dt=1`, any solver than Euler will need the value at timesteps that aren’t a multiple of dt.

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [January 17, 2025, 2:36pm UTC](https://discourse.julialang.org/t/accessing-current-time-point-inside-differential-equation-function-while-solving/124876/5 "2025-01-17T14:36:10Z")

</div>

example I solved one of function in my code for value of `t` and I got following results.

It was for ` tspan = (0, 10.0), dt = 0.01`

```julia

julia> sol = solve(problem, Rodas5P(), saveat = 0.01)
0.0
0.0
0.0
0.0
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(0.0,1.0)
0.006358126895828704
0.004095798393397535
0.009769306725060716
0.0042884036095586645
0.01
0.01
0.01
0.01
0.01
0.01
0.01
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(0.01,1.0)
0.07358126895828704
0.05095798393397535
0.10769306725060715
0.05288403609558664
0.10999999999999999
0.10999999999999999
0.10999999999999999
0.10999999999999999
0.10999999999999999
0.10999999999999999
0.10999999999999999
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(0.10999999999999999,1.0)
0.5280714823594383
0.3793146163686202
0.7523697751061996
0.3919791557132927
0.7675387519140539
0.7675387519140539
0.7675387519140539
0.7675387519140539
0.7675387519140539
0.7675387519140539
0.7675387519140539
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(0.7675387519140539,1.0)
1.5015098814683507
1.2403506071163937
1.8952905582926574
1.2625846188591567
1.9219213894103646
1.9219213894103646
1.9219213894103646
1.9219213894103646
1.9219213894103646
1.9219213894103646
1.9219213894103646
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(1.9219213894103646,1.0)
2.7957387521793122
2.4848198356335662
3.264547917385764
2.5112901758452213
3.2962528192253653
3.2962528192253653
3.2962528192253653
3.2962528192253653
3.2962528192253653
3.2962528192253653
3.2962528192253653
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(3.2962528192253653,1.0)
4.377133124012223
3.992537729598177
4.957033014956837
4.025280577949373
4.9962508257073965
4.9962508257073965
4.9962508257073965
4.9962508257073965
4.9962508257073965
4.9962508257073965
4.9962508257073965
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(4.9962508257073965,1.0)
6.236796668378545
5.795389589779867
6.902358244152333
5.832969148052323
6.94736923368541
6.94736923368541
6.94736923368541
6.94736923368541
6.94736923368541
6.94736923368541
6.94736923368541
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(6.94736923368541,1.0)
8.357986975483474
7.856065454319983
9.114793322134911
7.898796959923934
9.165975066676115
9.165975066676115
9.165975066676115
9.165975066676115
9.165975066676115
9.165975066676115
9.165975066676115
Dual{ForwardDiff.Tag{DiffEqBase.OrdinaryDiffEqTag, Float64}}(9.165975066676115,1.0)
9.696258702711948
9.507574864872261
9.980759605675049
9.523638620128922
10.0
10.0
10.0

```

I just want `t as increment of dt` so … `0.0, 0.01, 0.02, 0.03.....`  
how can I get this inside function while its solving?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 18, 2025, 10:38am UTC](https://discourse.julialang.org/t/accessing-current-time-point-inside-differential-equation-function-while-solving/124876/6 "2025-01-18T10:38:12Z")

</div>

> [@Sahil\_Khan](#):
>
> I just want `t as increment of dt` so … `0.0, 0.01, 0.02, 0.03.....`

That doesn’t necessarily exist for an adaptive method, nor does it always go forward, nor is it at evenly-spaced points.

Take a step back: what are you actually trying to do?

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [January 20, 2025, 9:37am UTC](https://discourse.julialang.org/t/accessing-current-time-point-inside-differential-equation-function-while-solving/124876/7 "2025-01-20T09:37:55Z")

</div>

In my model I have a function (lets say Y) which returns a vector output. This vector is basically change in some variable(lets say x) at equally spaced time-points till end of my simulation time.

Eg: Say Im simulating my model for 20 timepoints and in above function i extract values at interval = 0.1. So, 0.0 =\> x1, 0.1 =\> x2, … 20.0 =\> x21 where xn are different values of x calculated from function Y.

Now I want to feed this information in my differential equation function for those specific time points.

( **This is just an example function to show what i’m trying to do** )

```julia
function tmp!(du, u, p, t)
    
    i, = p
    quack, = u

    du[1] = x[t] * i * quack. ## x is variable that comes from other function but needed to be fed here at specific time points based on interval(0.1 in this example).

end

```

I was thinking can callback setup help in this scenario?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 20, 2025, 9:46am UTC](https://discourse.julialang.org/t/accessing-current-time-point-inside-differential-equation-function-while-solving/124876/8 "2025-01-20T09:46:13Z")

</div>

Either a callback or what you can do is use DataInterpolations.jl to put an interpolation over that data and evaluate it using t

---

<div class="post-metadata">

**Author:** ![Sahil\_Khan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahil_khan/32/47573_2.png) [@Sahil\_Khan](https://discourse.julialang.org/u/Sahil_Khan)\
**Post date:** [January 20, 2025, 10:13am UTC](https://discourse.julialang.org/t/accessing-current-time-point-inside-differential-equation-function-while-solving/124876/9 "2025-01-20T10:13:38Z")

</div>

> [@ChrisRackauckas](#):
>
> put an interpolation over that data and evaluate it using t

I don’t think I understood what you meant here.

Based on quick look, it feels interpolation package is basically helping to get a continuous output for your given data based on some method(making it more or less smooth). I don’t understand how it helps in above query.

Also when you say data. You mean variable` x` or output of `tmp()` in above shown example?

Did you mean something like this ?

```julia
x_t = LinearInterpolation(x, t)

function tmp!(du, u, p, t)
    
    i, = p
    quack, = u
    du[1] = x_t[?] * i * quack # it will still need some index based on current time.

end

```

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [January 20, 2025, 6:32pm UTC](https://discourse.julialang.org/t/accessing-current-time-point-inside-differential-equation-function-while-solving/124876/10 "2025-01-20T18:32:28Z")

</div>

du[1] = x\_t(t) \* i \* quack
