Differentiation question - How to find out H vs. dH/dt from a list of height and datetime series?
7 views (last 30 days)
Show older comments
Ashfaq Ahmed
on 14 Dec 2022
Commented: Star Strider
on 14 Dec 2022
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!
0 Comments
Accepted Answer
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)
DOY = day(DT,'dayofyear')
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
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
csDOY = cumsum(day(DT,'dayofyear')) % Cumulatively Sum 'dayofyear'
dT = gradient(csDOY,1) % Then, Calculate 'gradient'
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.
.
More Answers (0)
See Also
Categories
Find more on Ordinary 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!