% This is a MATLAB hint to calculate distances using the m_lldist function:
% assume that you have the following points digitize, where the first point
% is on the spreading center and the other points are moving out from that.
Ê
long=[130 131 133 135 140];
lat=[45 46 48 50 55];

%to calculate distances between adjacent points:
deltaDistance=m_lldist(long,lat);

% deltaDistance is a column vector of distances between adjacent points
Ê
% the following adds up the distances between points to determine the
% distance from the spreading center.Ê Since we let the spreading center be
% a zero distance I add a zero at the beginning:Ê remember that brackets []
% make the stuff in between a single vector.Ê since the variable range
% comes from m_lldist as a column vector, I have to put a semicolon ; after
% the zero to tell MATLAB to go on to the next line

xfromspreading=[0; cumsum(deltaDistance)]

% all of this could be put into a single function. Save the following 3
% lines (after removing the commenting character (%).Then make your
% vectors of latitudes and longitudes, pass them to this function and use
% the output in your analysis.
%
% xfromspreading=myfunctionfordistances(long, lat)
%      deltaDistance=m_lldist(long,lat);
%      xfromspreading=[0; cumsum(deltaDistance)];
Ê

% other bits of information:
% say that you have a vector of ages (T) and a vector of distances (D) from the
% ridge.Ê You can fit a line to all the points as:

coef=polyfit(T,D,1);

% the first element of coef is the slope, the second is the intercept.  The 1 tells polyfit to do a linear fit

% if you wanted to fit two segments (points 1 to 4) then separately points
% 5 to 9, the call would be:

coefFirstSeg=polyfit(T(1:4),D(1:4),1);
coefSecondSeg=polyfit(T(5:9), D(5:9),1);

% if you want to plot the  line through your points:

y_calc=polyval(coef,T);

% to plot the data (as o's) and the line for the fit:

plot(T,D,'o',T,y_calc)

% now polyfit is a good function to calculate polynomial fits to data.  
%However, it does not give any uncertainty estimates. 
%The function linreg is  available and with your estimates of uncertainty in your 
%data (both time uncertainty and location uncertainty) you can use linreg to estimate 
%the uncertainty in the slope which will be the uncertainty in the half-spreading rate.


[a,b,da,db]=linreg(x,y,dy)

