File Exchange

image thumbnail

Tidal fitting toolbox

version 1.3 (422 KB) by Aslak Grinsted
fit tidal components to an observed series of sea level and use them for prediction

24 Downloads

Updated 15 Dec 2014

View License

* Fitting of tidemodels to e.g. sea level data.
* Predictions of tide based on tide model.
Uses familar syntax from polyfit/polyval: tidalfit/tidalval

% TIDALFIT: Fits a tidal model to data

tidalfit uses the HAMELS (ordinary least squares)
technique to fit tidal components to the detrended data. Additionally it
can also do robust fitting.

Please include an acknowledgement to Aslak Grinsted if you use this code.

USAGE: tidal=tidalfit(data[,parameter,value])

INPUT:
------
data: A two column vector.
\ - first column should be a serial date number (See help datenum)
\ - second column should be the y-values (i.e. sea level)
\ (missing values and nans are OK.)

OPTIONAL PARAMETERS:
--------------------
Components: cell-array of strings with names of the which
\ components should be included in the fit? (ALL is default)
\ Note: The routine will only attempt to fit components
\ that have period<data_timespan/4 and period>dt*2.
FittingMethod: 'OLS' for ordinary least squares or 'ROBUST' for robustfitting.
\ (default=OLS)
RobustFitOptions: cell of options for robustfit. (See help robustfit.)
\ only used if FittingMethod='ROBUST'. (default={})
DetrendData: should the data be detrended prior to fitting? (default=true)

Note: optional parameters can be specified using abbreviations. e.g. RFO for RobustFitOptions.

OUTPUT:
-------
If no output arguments are specified the routine will display the results
visually.

tidal: A struct-array containing the fitted model parameters.
\ .name: name of tidal component (see e.g. http://www.mhl.nsw.gov.au/www/tide_glossary.htmlx)
\ .period:period of tidal component in days
\ .speed: frequency of tidal component in degrees per solar hour
\ .amp: amplitude of fitted component
\ .phase: phase of fitted component

Components that are not included in the fit will have NaN in .amp and .phase.



EXAMPLE:
data=datenum(1971,1,1):datenum(2008,1,1);
data=[data;randn(size(data))]';
tidal=tidalfit(data,'fm','robust');
future=[datenum(2008,1,1):datenum(2009,1,1)'];
plot(future,tidalval(tidal,future));

Cite As

Aslak Grinsted (2019). Tidal fitting toolbox (https://www.mathworks.com/matlabcentral/fileexchange/19099-tidal-fitting-toolbox), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (19)

Dear Aslak Grinsted,

Do you have documentation about your matlab script? I want to learn it deeply. Because, I have tried your script and the results is quite better than another. Thanks

Mary Zhu

@ Olga the same question I encountered.

Thanks a lot for such a userfriendly skript!

Is is necessary to convert sample-time in UTS?
I suppose it could be impotrant for the phase calculation, isnt it?

I have that question, because my calculation results (input time in UTS) for O1 and M2 phases differ very from the information for studien region.

@Darina: I am not sure how you triggered the error. It works for me when i do like this:
tidalfit(data,'Components',{'m4' 'm6'});

Thank you for such a useful tidal package. Very pleased that the program has the ability to use only the required components for calculations. But unfortunately I've got an error at line 179, when trying to change the component parameters. It looks like that:
Error in ==> tidalfit at 179
ix=strmatch(upper(Args.Components{ii}),components,'exact');

Is it possible to fix it?

Also, could you explain, please what does warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering
and scaling as described in HELP POLYFIT mean?

Zhang Xilei

Thanks a lot, save me much time with better and optional resolutions.

@janez: the reason tidalfit has larger residuals is probably that it accounts for a lineal sea level rise trend. I believe that this results in a total model that scores better in an out of sample validation.

janez

Very useful package and very easy to use. Worked right from the start with no problems. I also did a quick check and compared the results with t_tide package by R. Pawlowicz. Tidal forecasts from both packages were generated using the same 1-year (whole year 2011) timeseries of sea level measurements and compared to the the same measurements (whole year 2012). RMSE value of residuals (observed sea level - astronomical tide) were 16.04 cm (tidal_fit) and 15.30 cm (t_tide). The maximum/minimum times differ for up to 4 minutes. Qualitative and quantitative curve behavior is the same in both packages for the most time. As a rule of thumb i'd say the performance of both packages is very similar and consistent.

can any budy help me with this program pleas

Aslak Grinsted

Thanks for the comments. I've just improved the code and help significantly along the lines of the propositions.

w s

Carlos, you are absolutely right. I think in general, however, one should not take the rating texts too verbal. Everything, that is not excellent needs improvement. Hence a fair and good actually do not exist. I just tried to find something inbetween since there are many poor submissions that are rated a two. And this one here is much better.

Carlos Adrián Vargas Aguilera

That's why the 2 stars: "Needs Improvements", It doesn't mean that it shouldn't be downloaded, but if you do so, you'll have to do a little research on some web pages written somewhere by the author to know exactly how the analysis is done. That's all.
In my opinion the program should speak by itself. There are several Toolboxes on Tides on the web and they are totally explained, why this one not? When it does it, then an "Excellent" won't be a problem.

w s

I think that the first rating is too low. IMO this submission meets is all criteria of a GOOD submission. There is a help, commenting and vectorized code. I did not check on correctness of results since I am not familiar with tidal modelling.

Why is it only good?

First, there are some lines that Aslak probably forgot to remove before submitting the file, e.g.

if nargin<1
data=readsl('d242a.dat');
end

This should be removed since neither the function nor the file are available.

Second, as the first reviewer noticed the help needs some improvement. I did not understand the output until looking at the code itself, where a reference to a huge number of parameters is given. This should be given in the help itself.

Third, there is no error checking. tidalfit also works with tidalfit(magic(5)) which most likely produces nonsense.

Aslak Grinsted

I do not use polyfit to fill nans (i use it to detrend the data prior to fitting). and i do not use the t_tide toolbox. The reason i acknowledge him is because i used some ideas i got from one of his pdf's.

Carlos Adrián Vargas Aguilera

The help needs some improvements, for example, it is important to say that you use POLYFIT to fill NaN's, and other things, such as why define constants from a product of constants?. But, my question is: do you know the T_TIDE toolbox by Rich Pawlowicz? It will be usefull to compare both toolboxes.

Updates

1.3

added example html

1.0.0.0

mathworks didn't update the file correctly last time, so i have to re-upload it again. (It is still the old zip file but description has changed.)

Cleared all mlint messages.

MATLAB Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux