How to use a data from time series (e.g.) in ode function?

2 次查看(过去 30 天)
Hi, i want to use data from another mat file which contains a timeseries, below is a simplified example:
tspan = [0 5];
y0 = 0;
tQ = linspace(0,5,25);
Qg = load('GasFlowRate_T.mat', 'Qg');
[t y] = ode45(@(t,y) f(t,y,tQ,Qg),tspan,y0);
plot(t,y)
function dydt = f(t,y,tQ,Qg)
Qg = interp1(tQ, Qg, t);
dydt = Qg*t;
end
and i will get the error:
Error using interp1>reshapeValuesV (line 439)
Values V must be of type double or single.
Error in interp1>reshapeAndSortXandV (line 419)
[V,orig_size_v] = reshapeValuesV(V);
Error in interp1 (line 93)
[X,V,orig_size_v] = reshapeAndSortXandV(varargin{1},varargin{2});
but i also couldn't convert from struct to double, how could i exactly to use the data from timeseries?

回答(1 个)

Stephen23
Stephen23 2020-2-20
You just need to get the numeric array out of the structure, e.g.:
S = load('GasFlowRate_T.mat', 'Qg');
Qg = S.Qg;
  1 个评论
Wenqing Qiu
Wenqing Qiu 2020-2-20
You're right, but if i only use this code, i will stil get error because of my ode function:
Error using odearguments (line 113)
Inputs must be floats, namely single or double.
but i just tried to save my data in array, the everything works.
Through thank a lot for your answer:)

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by