Differentiation question - How to find out H vs. dH/dt from a list of height and datetime series?

6 views (last 30 days)
Hi!
I have a fundamental differentiation problem. I need to find out the derivative of the tide height (H) with repsect to time (t). The heights are in meter and the times are in datetime format.
H = [1.164;0.819;0.389;0.023;-0.123;-0.081;-0.039;...
0.118;0.272;0.515;0.892;1.301;1.572;1.573;1.576];
and the time series, T is -
'01-Jan-2003'
'17-Jan-2003'
'03-Feb-2003'
'20-Feb-2003'
'01-Mar-2003'
'15-Mar-2003'
'04-Apr-2003'
'16-Apr-2003'
'30-Apr-2003'
'15-May-2003'
'01-Jun-2003'
'16-Jun-2003'
'03-Jul-2003'
'18-Jul-2003'
'05-Aug-2003'
Can anyone please give me an idea on how can I plot the H vs. dH/dt? Any feedback will be greatly appreciated!

Accepted Answer

Star Strider
Star Strider on 14 Dec 2022
One approach —
T = ['01-Jan-2003'
'17-Jan-2003'
'03-Feb-2003'
'20-Feb-2003'
'01-Mar-2003'
'15-Mar-2003'
'04-Apr-2003'
'16-Apr-2003'
'30-Apr-2003'
'15-May-2003'
'01-Jun-2003'
'16-Jun-2003'
'03-Jul-2003'
'18-Jul-2003'
'05-Aug-2003'];
H = [1.164;0.819;0.389;0.023;-0.123;-0.081;-0.039; 0.118;0.272;0.515;0.892;1.301;1.572;1.573;1.576];
DT = datetime(T)
DT = 15×1 datetime array
01-Jan-2003 17-Jan-2003 03-Feb-2003 20-Feb-2003 01-Mar-2003 15-Mar-2003 04-Apr-2003 16-Apr-2003 30-Apr-2003 15-May-2003 01-Jun-2003 16-Jun-2003 03-Jul-2003 18-Jul-2003 05-Aug-2003
DOY = day(DT,'dayofyear')
DOY = 15×1
1 17 34 51 60 74 94 106 120 135
dHdT = gradient(H) ./ gradient(DOY);
figure
yyaxis left
plot(DT, H)
ylabel('Height', 'Interpreter','latex')
yyaxis right
plot(DT, dHdT)
ylabel('$\frac{dH(T)}{dT}$', 'Interpreter','latex')
grid
.
  2 Comments
Ashfaq Ahmed
Ashfaq Ahmed on 14 Dec 2022
This is a brilliant approach! Thank you so much for the code! But can you please tell me what happens when I work with multiple year's data? I think the only limitation of this code can be the conversion to the "date of the year" array. How can I proceed with when I have many year's of data?
Star Strider
Star Strider on 14 Dec 2022
As always, my pleasure!
One option is to use datenum, however as much as I would like it to be possible to do this with a duration array, that does not appear to be an option, although it should be possible.
One way to hack it —
T = ['01-Jan-2003'
'17-Jan-2003'
'03-Feb-2003'
'20-Feb-2003'
'01-Mar-2003'
'15-Mar-2003'
'04-Apr-2003'
'16-Apr-2003'
'30-Apr-2003'
'15-May-2003'
'01-Jun-2003'
'16-Jun-2003'
'03-Jul-2003'
'18-Jul-2003'
'05-Aug-2003'];
DT = reshape(datetime(T) + calyears(0:2), [],1) % Create Date Data For Multiple Years
DT = 45×1 datetime array
01-Jan-2003 17-Jan-2003 03-Feb-2003 20-Feb-2003 01-Mar-2003 15-Mar-2003 04-Apr-2003 16-Apr-2003 30-Apr-2003 15-May-2003 01-Jun-2003 16-Jun-2003 03-Jul-2003 18-Jul-2003 05-Aug-2003 01-Jan-2004 17-Jan-2004 03-Feb-2004 20-Feb-2004 01-Mar-2004 15-Mar-2004 04-Apr-2004 16-Apr-2004 30-Apr-2004 15-May-2004 01-Jun-2004 16-Jun-2004 03-Jul-2004 18-Jul-2004 05-Aug-2004
csDOY = cumsum(day(DT,'dayofyear')) % Cumulatively Sum 'dayofyear'
csDOY = 45×1
1 18 52 103 163 237 331 437 557 692
dT = gradient(csDOY,1) % Then, Calculate 'gradient'
dT = 45×1
17.0000 25.5000 42.5000 55.5000 67.0000 84.0000 100.0000 113.0000 127.5000 143.5000
H = sin(2*pi*csDOY*(0.25/365))*1.5; % Simulate Tide Height
dH = gradient(H,(1/365)); % Then, Calculate 'gradient'
figure
yyaxis left
plot(DT, H)
ylabel('Height', 'Interpreter','latex')
yyaxis right
plot(DT, dH./dT)
ylabel('$\frac{dH(T)}{dT}$', 'Interpreter','latex')
grid
There should be a more straightforward way to calculate cumulative days across multiple years using datetime or duration arrays. If there is, it’s quite well hidden in the documentation, at least from me.
In this instance, it might still be easier to convert the datetime array to a datenum array before calculating the gradient. That would be worth considering.
My apologies for the delay, however it took a while to come up with this approach.
.

Sign in to comment.

More Answers (0)

Categories

Find more on Numerical Integration and Differential Equations 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!