# Add time dependent terms in a differential equation

**URL:** <https://discourse.julialang.org/t/add-time-dependent-terms-in-a-differential-equation/113338>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [April 22, 2024, 2:07pm UTC](https://discourse.julialang.org/t/add-time-dependent-terms-in-a-differential-equation/113338 "2024-04-22T14:07:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Piyush](https://avatars.discourse-cdn.com/v4/letter/p/ecb155/32.png) [@Piyush](https://discourse.julialang.org/u/Piyush)\
**Post date:** [April 22, 2024, 2:07pm UTC](https://discourse.julialang.org/t/add-time-dependent-terms-in-a-differential-equation/113338/1 "2024-04-22T14:07:31Z")

</div>

Hi,

I am a new to Julia and trying to solve Differential Equation with exogenous inputs. The inputs will be read from data at discrete times. Can someone help with the minimal code for the same. I would also like to do the parameter estimation for the same.

---

<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:** [April 22, 2024, 3:26pm UTC](https://discourse.julialang.org/t/add-time-dependent-terms-in-a-differential-equation/113338/2 "2024-04-22T15:26:07Z")

</div>

How is your exogenous input defined? An array of values? If so, just use DataInterpolations.jl to define the function. With that, did you get a good ODE definition? Would be good to see your steps here.

---

<div class="post-metadata">

**Author:** ![Piyush](https://avatars.discourse-cdn.com/v4/letter/p/ecb155/32.png) [@Piyush](https://discourse.julialang.org/u/Piyush)\
**Post date:** [April 22, 2024, 3:31pm UTC](https://discourse.julialang.org/t/add-time-dependent-terms-in-a-differential-equation/113338/4 "2024-04-22T15:31:15Z")

</div>

Hi Chris, thank you for your response. I did try using interpolation before but I could not get it right. I had been reading other posts as well. Could you help me how to use DataInterpolations.jl as well for the code below.

Here is the code that I have put up so far:

here rate(t) is the exogenous input that I am trying to read from a data file.

```julia
function rate(t)
if t > feedTime[end]
rate = 0
else
for i in 1:(length(feedTime) - 1)
if feedTime[i] < t < feedTime[i+1]
rate = feedData.Total[i]
end
end
end
return rate
end

function Volume(du, u, p, t)
du = u + rate(t)
end

u0 = [1.0]
tspan = (0.0, 14.0)

prob = ODEProblem(Volume, u0, tspan, rate)

sol = solve(prob, Tsit5(), tstops= feedTime)

display(plot(sol))

```

---

<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:** [June 15, 2024, 10:58am UTC](https://discourse.julialang.org/t/add-time-dependent-terms-in-a-differential-equation/113338/5 "2024-06-15T10:58:04Z")

</div>

You didn’t share data so I just set this up with random data.

```julia
using DataInterpolations, OrdinaryDiffEq
feedTime = Array(0:0.2:14.0)
feedData = [rand(1) for i in 1:length(feedTime)]
rate = CubicSpline(feedData, feedTime)

function Volume(du, u, p, t)
    du .= u + rate(t)
end

u0 = [1.0]
tspan = (0.0, 14.0)
prob = ODEProblem(Volume, u0, tspan, rate)
sol = solve(prob, Tsit5(), tstops= feedTime)

```
