function error=cal_error(stla,stlo,time,baz,evla,evlo,torigin,Erad,trefgvel)

% function designed to calculate error by comparing difference in time and 
% back azimuth.
% Preferred norm is selected by referring to Kennett(1996)

% Epicentral distance from observed time.
obsepi(1)=(time(1)-torigin)*24*3600*trefgvel;
obsepi(2)=(time(2)-torigin)*24*3600*trefgvel;
obsepi(3)=(time(3)-torigin)*24*3600*trefgvel;

% Epicentral distances from trial epicenter.
tmpepi(1)=cal_epi1(stla(1),stlo(1),evla,evlo);
tmpepi(2)=cal_epi1(stla(2),stlo(2),evla,evlo);
tmpepi(3)=cal_epi1(stla(3),stlo(3),evla,evlo);
tmpepi=tmpepi.*Erad*pi/180;

% Back azimuths from trial epicenter.
tmpbaz(1)=cal_baz(stla(1),stlo(1),evla,evlo);
tmpbaz(2)=cal_baz(stla(2),stlo(2),evla,evlo);
tmpbaz(3)=cal_baz(stla(3),stlo(3),evla,evlo);

% Calculate Error by combining difference in epicenter and backazimuth.

alpha = 0.1;
n=3;
errepi=(1-alpha).*(obsepi'-tmpepi').^2./n;
errbaz=alpha.*((baz'-tmpbaz').*pi/180.*tmpepi').^2./n;
error=sum(sqrt((errepi+errbaz)).^(1.25));
