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

2 views (last 30 days)
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?

Answers (1)

Stephen23
Stephen23 on 20 Feb 2020
You just need to get the numeric array out of the structure, e.g.:
S = load('GasFlowRate_T.mat', 'Qg');
Qg = S.Qg;
  1 Comment
Wenqing Qiu
Wenqing Qiu on 20 Feb 2020
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:)

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!