      subroutine ellipse(xm,sgxm,sgym,eigvs,prob,iud) 
c bernd berg aug 7 1999.
c purpose: prepare confidence ellipse for gnuplot.
      include '../../libs/fortran/implicit.sta'
      include '../../libs/fortran/constants.par'
      parameter (ngnu=100)
      dimension xm(2),sgxm(2),sgym(2),eigvs(2,2)
c
c 1. ellipse:
c prob<0: standard covariance ellipse with 39% confidence.
      open(unit=iud,file='ellipse1.d',status='unknown')
      if(prob.lt.zero)           then
      r=one
                                 else
      r=sqrt(-two*log(one-prob))
                                 end if
      delphy=tpi/ngnu
      do i=0,ngnu
      phy=i*delphy
      y1=+r*sgym(1)*sin(phy)
      y2=+r*sgym(2)*cos(phy)
      x1=xm(1)+eigvs(1,1)*y1+eigvs(1,2)*y2
      x2=xm(2)+eigvs(2,1)*y1+eigvs(2,2)*y2
      write(iud,*)  x1,x2
      end do
      close(iud)
c
      open(unit=iud,file='ellipse2.d',status='unknown')
c 2. standard deviations in orignal directions:
      x1=xm(1)+sgxm(1)
      write(iud,*)  x1,xm(2)
      x1=xm(1)-sgxm(1)
      write(iud,*)  x1,xm(2)
      x1=xm(1)
      write(iud,*)  x1,xm(2)
      x2=xm(2)+sgxm(2)
      write(iud,*)  xm(1),x2
      x2=xm(2)-sgxm(2)
      write(iud,*)  xm(1),x2
      write(iud,*)  xm(1),xm(2)
c
c 3. standard deviations in the independent directions:
      write(iud,*) 'set texture dashes'
      do i=1,2
      x1=xm(1)+sgym(i)*eigvs(1,i)
      x2=xm(2)+sgym(i)*eigvs(2,i)
      write(iud,*)  x1,x2
      x1=xm(1)-sgym(i)*eigvs(1,i)
      x2=xm(2)-sgym(i)*eigvs(2,i)
      write(iud,*)  x1,x2
      write(iud,*)  xm(1),xm(2)
      end do
      close(iud)
c
      return
      end
