      subroutine fit_lgnu(iud,n,x,y,ey,a,sga,cov,prob)
c bernd berg dec 16 2000. 
c prob: confidence level for the ellipse.
      include '../../libs/fortran/implicit.sta'
      parameter(nfig=200)
      dimension x(n),y(n),ey(n), a(2),sga(2),cov(2,2)
      dimension covdd(2),sgb(2),eigvs(2,2)
c
c 1. regression line
c
      open(unit=iud,file="lfit1.d",status='unknown')
      do i=1,n
        write(iud,'(1x,4g16.7)') x(i),y(i),ey(i)
      end do
      close(iud)
c
c 2. confidence ellipse:
c
      call eigen_2x2(cov,covdd,eigvs)
      sgb(1)=sqrt(covdd(1))
      sgb(2)=sqrt(covdd(2))
      call ellipse(a,sga,sgb,eigvs,prob,iud)
c 
c 3. include confidence limits for regression line:
c
      delx=(x(n)-x(1))/nfig
c
      open(unit=iud,file="lfit2.d",status='unknown')
      do i=0,nfig
        xx=x(1)+i*delx
        varb1=abs(eigvs(1,1)+xx*eigvs(2,1))**2*covdd(1)
        varb2=abs(eigvs(1,2)+xx*eigvs(2,2))**2*covdd(2)
        ym=a(1)+a(2)*xx-sqrt(varb1+varb2)
        yy=a(1)+a(2)*xx
        yp=a(1)+a(2)*xx+sqrt(varb1+varb2)
        write(iud,'(1x,4g16.7)') xx,ym,yy,yp
      end do
      close(iud)
c
      return
      end
