function [EMAX, EMIN, AZMAX] = errlps(covm, P)
%
% Matlab function to compute error ellipses for point
% positioning given covariance matrix covm should be 4x4
% Paul Segall Jan 18, 1995;  modified Jan 9, 1997
% 
% Input:
%	covm	=  position covariance matrix
%	P	=  probability level (ie, 0.95)
% Output:
%	EMAX	=  semi-major axis of error ellipse
%	EMIN	=  semi-minor axis of error ellipse
%	AZMAX	=  orientation of major axis (counter clockwise
%			from East)
%

hor = covm(1:2, 1:2);
[eigenvecs,eigenvals] = eig(hor);
[maxval, index]  = max(diag(eigenvals));
minval = min(diag(eigenvals));
maxvec = eigenvecs(:, index);
AZMAX =  atan2(maxvec(2), maxvec(1))*180/pi;

chi2p = chi2inv(P,2);
EMAX = sqrt(chi2p*maxval);
EMIN = sqrt(chi2p*minval);
