11
$\begingroup$

I've got a bunch of readings at various dates and times. I'd like to be able to interpolate and then integrate over, say days. I can do this by converting my date/time to AbosluteTime and get the interpolating function that way. Then by integrating and plugging in seconds as my limits of integration I get what I want.

Also, since seconds in really finer than I need, I convert all of the AbsoluteTimes to the number of hours since the first reading.

I was wondering if there might be a way to do this directly using dates. For what it's worth, my readings last for about 2 months and the values are between about 90 and 300.

SAMPLE DATA

 x[[1 ;; 5]]

{{{2014, 8, 4, 10, 36, 0.}, 257.},{{2014, 8, 4, 16, 28, 0.}, 385.},

{{2014, 8, 4, 22, 53, 0.}, 176.}, {{2014, 8, 5, 6, 52, 0.}, 148.}, {{2014, 8, 5, 11, 19, 0.}, 192.}}

$\endgroup$
3
  • 2
    $\begingroup$ Could you please add an example of your data? $\endgroup$
    – rhermans
    Sep 2, 2014 at 17:45
  • $\begingroup$ Sorry for the wrong edit. Anyway, a +1 question :) $\endgroup$
    – eldo
    Sep 2, 2014 at 18:58
  • $\begingroup$ @eldo no problem about the edit. I appreciate your looking over the question. $\endgroup$ Sep 2, 2014 at 20:38

2 Answers 2

14
$\begingroup$

There's a couple ways to do this:

data = {{{2014, 8, 4, 10, 36, 0.}, 257.}, {{2014, 8, 4, 16, 28, 0.}, 385.},
 {{2014, 8, 4, 22, 53, 0.}, 176.}, {{2014, 8, 5, 6, 52, 0.}, 148.},
 {{2014, 8, 5, 11, 19, 0.}, 192.}};

1) Convert dates to absolute times:

data[[All, 1]] = AbsoluteTime /@ data[[All, 1]];
f1 = Interpolation@data;

f1[AbsoluteTime@{2014,8,4,10,40}]

261.669

2) With TimeSeries (v10)

ts = TimeSeries[data];
ts[{2014, 8, 4, 10, 40}]

258.455

The TimeSeries interpolation uses first order by default but can be changed by changing the ResamplingMethod -> {"Interpolation", opts} option in the TimeSeries function.

ts = TimeSeries[data, ResamplingMethod -> {"Interpolation", InterpolationOrder -> 3}];
ts[{2014, 8, 4, 10, 40}]

261.669

To integrate over:

NIntegrate[ts[t], {t, AbsoluteTime[{2014, 8, 4, 10, 40}], AbsoluteTime[{2014, 8, 4,11}]}]
$\endgroup$
3
  • 1
    $\begingroup$ Looks like TimeSeries was a nice addition to version 10 - I like that method. $\endgroup$ Sep 2, 2014 at 20:54
  • 2
    $\begingroup$ Incidentally, with TimeSeries it isn't necessary to use the "PathFunction". Just evaluate directly on the date stamp. $\endgroup$
    – Andy Ross
    Sep 3, 2014 at 12:16
  • $\begingroup$ @AndyRoss Sweet. Didn't know that. $\endgroup$
    – kale
    Sep 3, 2014 at 15:47
2
$\begingroup$

Some sample data:

r = # - #[[1]] &@(Range[##, 3690] & @@ (AbsoluteTime[{#, {"Day", "Month", "YearShort"}}] & /@ 
                                                                         {"05/01/14", "05/03/14"}));
hours = N[r/3600];
data = Transpose[{hours, hours^2}];

Integrate:

f = Interpolation@data;
Integrate[f@x, {x, 0, Last@hours}]

(*9.45434*10^8*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.