function [Qn,Rn]= triangdb(An,N)
%
% The periodic sequence A(n), n=1...N, is triangularized.
% Sequences Q(n) of orthogonal matrices and R(n) of upper triangular
% matrices are computed such that Q(n+1)R(n) = A(n)Q(n) holds up
% to rounding errors. 
% (It is assumed that the sequence A(n) is hyperbolic).
%
% input:  An(N,4)		sequence of 2x2-matrices. An(n,:) contains
% 				An11, An21, An12 and An22.
%				The matrices are obtained by 
%				reshape(An(n,:),2,2)).
%	  N 			period of pseudo orbit
% output: Qn(N,4)		interval. Sequence of orthogonal 2x2-matrices  
%				of the form [an, bn; bn; -an]. 
%				The matrices are obtained by 
%				reshape(Qn(n,:),2,2)).
%	  Rn(N,4)		sequence of triangular 2x2-matrices R(n). 
%				The matrices are obtained by 
%				reshape(Rn(n,:),2,2)). 
%				Rn is a point variable.
%
  stop=1;
  U1resh=[1,0,0,1];
  while stop>1e-14,
    Un=U1resh;
    Rn=[];
    for i=1:N,
      [U1,R1]=qr(reshape(An(i,:),2,2)*reshape(Un(i,:),2,2));
      U1resh=reshape(U1,1,4);
      Un=[Un;U1resh];
      Rn=[Rn;reshape(R1,1,4)];
    end;
    I1=reshape(Un(1,:),2,2)'*U1;
    I2=abs(I1)-eye(2);
    stop=norm(I2);
  end
  Un(:,3)=Un(:,2);
  Un(:,4)=-Un(:,1);
  Qn=intval(Un(1:N,:));
%
% written   Sept/10/02   D.Stoffer
%
