# How can I add values of an array as an input into the ode45 function ?

9 views (last 30 days)
Ece Kurt on 13 Nov 2019
Answered: Ece Kurt on 13 Nov 2019
This is my current profile and I want to calculate the SOC of the battery based on this current profile. So, I have the array of the current values of each time.
I wrote a function like,
I=Current.mat;
Q=2.4;
tspan=[1 1370];
y0=[0];
[t,y]=ode45(@soc,tspan,[y0],[],Q,I);
function dydt=soc(t,y,Q,I)
dydt= (-I(t)/(3600*Q)); %Here I(t), is an array form. 1370x1 double
end
%----------but I get this error whenever I run the code.
Array indices must be positive integers or logical values.
Error in Ode_example>soc (line 46)
dydt= (-I(t)/(3600*Q));
Error in odefcncleanup>@(t,y)oldFcn(t,y,inputArgs{:}) (line 11)
newFcn = @(t,y) oldFcn(t,y,inputArgs{:});
Error in ode45 (line 299)
f2 = odeFcn_main(t2, y2);
Error in Ode_example (line 32)
[t,y]=ode45(@soc,tspan,[y0],[],Q,I);
Plese help me. Why I always get this error? Is there any other way to implement an array as a input to function?

James Tursa on 13 Nov 2019
Edited: James Tursa on 13 Nov 2019
What is the size of I? What is the DE you are solving? Maybe just dropping the (t) will work:
dydt= (-I/(3600*Q));
Or maybe you need to interpolate I based on the value of t. It is hard to know how to advise without knowing what DE you are solving and exactly what I is.

Stephen Cobeldick on 13 Nov 2019
this is the DE. dydt is represents the .
I(t) current and I have just values of it. It's not an equation form. Its an array. I have the data of the input current of the battery. So, I just load it to the script and now I want to use it for SOC calculation. Size of the current data is 1370x1 double. Thanks
dydt= (-I/(3600*Q));
Undefined unary operator '-' for input arguments of type 'function_handle'.
James Tursa on 13 Nov 2019
If I is an array of values at specific times, then you will need to interpolate I inside your derivative function to get the value of I at the specific time t that the ode45 solver wants. I must cover the entire range of time of the integration. What are the time values corresponding to I? You will need that to do the interpolation.
Another simpler option may be to just use cumtrapz( ) on your-I/(3600*Q) array to get your SOC values. You will need the time points of I to do this correctly also.

Ece Kurt on 13 Nov 2019
Okay! I think it works. Thank you all!!
current_index=1:1:1370;
I=@(t)interp1(current_index,Current,t);
tspan=[1 1370];
y0=0;
[t,y]=ode45(@soc,tspan,[y0],t,Q,I);
function dydt=soc(t,y,Q,I)
dydt= (I(t)/(-3600*Q));
end