# Input signal for solving system of ODEs

**URL:** https://discourse.julialang.org/t/input-signal-for-solving-system-of-odes/33982
**Category:** New to Julia
**Created:** [January 30, 2020, 3:05pm UTC](https://discourse.julialang.org/t/input-signal-for-solving-system-of-odes/33982 "2020-01-30T15:05:21Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Matthew\_Overlin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/matthew_overlin/32/10533_2.png) [@Matthew\_Overlin](https://discourse.julialang.org/u/Matthew_Overlin)
#### Post date: [January 30, 2020, 3:05pm UTC](https://discourse.julialang.org/t/input-signal-for-solving-system-of-odes/33982/1 "2020-01-30T15:05:21Z")

</div>

Hello. I currently have a working solution, but it is quite slow because of the way I have implemented it.

Before I perform an ODE integration, I establish input signals VD, VQ at times given in time\_V. time\_V, VD, and VQ are arrays. I have defined a function “function ODEs(du,u,p,t)” which returns the time derivatives of the states. During an ODE integration, this “function ODEs(du,u,p,t)” function will be called very frequently, at several times t. The input arrays VD, VQ are only defined at times in my time\_V array, though. So, if the time t is not a time in time\_V, I will find the closest time in time\_V and just use that. So, I find an index in the time\_V array, give the current time instant t, with the following line:  
Cart\_Index = argmin(broadcast(abs, time\_V - t\*ones(length(time\_V), 1)));

Then with the array index which is returned, I can use it to pick out the inputs VD[Cart\_Index], VQ[Cart\_Index] at the right time, and write the ODEs with these inputs (and other state variables, constants, etc.).  
Instead of the argmin(…) way to retrieve the index, I was looking for suggestions on a faster/better/more efficient way to do this.

Thanks,  
Matt

Thanks,  
Matt

---

<div class="post-metadata">

### Author: ![tomaklutfu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomaklutfu/32/2411_2.png) [@tomaklutfu](https://discourse.julialang.org/u/tomaklutfu)
#### Post date: [January 30, 2020, 7:18pm UTC](https://discourse.julialang.org/t/input-signal-for-solving-system-of-odes/33982/2 "2020-01-30T19:18:06Z")

</div>

Is `time_V` sorted? And is there a structure to it?. Then, close to what you want is

```julia
cart_index = findfirst(>=(t), time_V)

```

or if `time_V` has known time step, then, by having an appropriate `epsilon`

```julia
cart_index = findfirst(tv->abs(t-tv)<epsilon, time_V)

```
