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

조회 수: 14 (최근 30일)
Ece Kurt
Ece Kurt 2019년 11월 13일
답변: Ece Kurt 2019년 11월 13일
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,
load('Current.mat');
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
James Tursa 2019년 11월 13일
편집: James Tursa 2019년 11월 13일
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.
  댓글 수: 2
Stephen23
Stephen23 2019년 11월 13일
Ece Kurt's "Answer" moved here:
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
James Tursa 2019년 11월 13일
편집: James Tursa 2019년 11월 13일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ece Kurt
Ece Kurt 2019년 11월 13일
Okay! I think it works. Thank you all!!
load('Current.mat'); %1370x1 double
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

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by