# Input signal for solving system of ODEs

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

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

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

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

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