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


Updated 15 Dec 2014

View Version History

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])

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.)

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.

If no output arguments are specified the routine will display the results

tidal: A struct-array containing the fitted model parameters.
\ .name: name of tidal component (see e.g.
\ .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.


Cite As

Aslak Grinsted (2020). Tidal fitting toolbox (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (20)

Auwal Garba Abubakar


John Callahan


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

Kenneth Scott

Edward Robinson

Mary Zhu

@ Olga the same question I encountered.

Olga Kuznetsova

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.

Aslak Grinsted

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

Darina Bublik

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

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.

Aslak Grinsted

@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.


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.

madamode mode

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

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.

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

Community Treasure Hunt

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

Start Hunting!