      program minimum
c hladanie minima
      dimension xray(101),yray(101),yray1(101),yray2(101)
      parameter (r=0.6180339, cr=1.0-r)
c      parameter (r=0.25, cr=1.0-r)
      external funct
      x=0.25
      a=0.25
      do i=1,101
         xray(i)=x
         yray(i)=funct(x)
         x=x+0.1
      end do
c
      call metafl('cons')
      CALL SETPAG('DA4L')
      CALL DISINI
      CALL TITLIN('trojbodova metoda',1)
      CALL AXSLEN(2000,700)
      CALL AXSPOS(200,800)
      CALL GRAF(0.,5.,-1.,1.,-4.0,3.0,-3.0,1.0)
c      CALL GRAF(2.20,2.30,2.2,0.01,-4.0,3.0,-3.0,1.0)
           NX=NXPOSN(1.0)
           NY=NYPOSN(1.3)
      CALL MESSAG('Normalne delenie',NX,NY)      
      CALL COLOR('yellow')
      CALL curve(xray,yray,101)
      call color('red')
      a=0.0
      c=1.0
      b=0.5
  10  continue
      fa=funct(a)
      fb=funct(b)
      fc=funct(c)
      if ((fa.GT.fb).AND.(fb.GT.fc)) then
         NX=NXPOSN(a)
      NY=NYPOSN(fa)
      CALL SYMBOL(15,NX,NY)
      call messag('a',nx,ny+20)
      NX=NXPOSN(b)
      NY=NYPOSN(fb)
      CALL SYMBOL(15,NX,NY)
      call messag('b',nx,ny+20)
      NX=NXPOSN(c)
      NY=NYPOSN(fc)
      CALL SYMBOL(15,NX,NY)
      call messag('c',nx,ny+20)
         a=c
         c=c+1.0
         if (c.GT.100.0) then
            write(*,*) 'minimum sa nenaslo'
            go to 30
         end if
         b=0.5*(c+a)
         go to 10
      end if
      call color('green')
      CALL HSYMBL(20)
      jx=2400
      jy=200
      i=0
  20  continue
c      if ((fa.GT.fb).AND.(fb.LT.fc)) then
      if (fa.GT.fc) then
         a=b
      else
         c=b
      end if
      b=0.5*(a+c)
      if (abs(c-a).LE.0.001) then
         write(*,*) 'minimum je ',b,'  2.2135'
         go to 25
      end if  
      ai=real(i)  
      call number(ai,-1,jx-100,jy)
      call number(b,5,jx,jy)
      i=i+1
      jy=jy+50
      fa=funct(a)
      fb=funct(b)
      fc=funct(c)
      NX=NXPOSN(a)
      NY=NYPOSN(fa)
      CALL SYMBOL(21,NX,NY)
      NX=NXPOSN(b)
      NY=NYPOSN(fb)
      CALL SYMBOL(21,NX,NY)
      NX=NXPOSN(c)
      NY=NYPOSN(fc)
      CALL SYMBOL(21,NX,NY)
      go to 20
  25  NX=NXPOSN(0.5)
      NY=NYPOSN(-3.5)
      CALL MESSAG('minimum = ',NX,NY)
      NX=NXPOSN(1.7)
      call number(b,4,nx,ny)
      nx=nxposn(2.7)
      CALL MESSAG('/ presne = 2.2135',NX,NY) 
c
  30  continue
      call endgrf
      call color('white')
      CALL TITLIN('zlaty rez',1)
      CALL AXSLEN(2000,700)
      CALL AXSPOS(200,1800)
      CALL GRAF(0.,5.,-1.,1.,-4.0,3.0,-3.0,1.0)      
c      CALL GRAF(2.20,2.30,2.2,0.01,-4.0,3.0,-3.0,1.0)
       NX=NXPOSN(1.0)
       NY=NYPOSN(1.3)
      CALL MESSAG('Zlaty rez',NX,NY)
      CALL COLOR('yellow')
      CALL curve(xray,yray,101)
      call color('red')
      a=0.25
      c=1.0
      b=0.5
  11  continue
      fa=funct(a)
      fb=funct(b)
      fc=funct(c)
      if ((fa.GT.fb).AND.(fb.GT.fc)) then
         NX=NXPOSN(a)
         NY=NYPOSN(fa)
         CALL SYMBOL(15,NX,NY)
         call messag('a',nx,ny+20)
         NX=NXPOSN(b)
         NY=NYPOSN(fb)
         CALL SYMBOL(15,NX,NY)
         call messag('b',nx,ny+20)
         NX=NXPOSN(c)
         NY=NYPOSN(fc)
         CALL SYMBOL(15,NX,NY)
         call messag('c',nx,ny+20)
         a=c
         c=c+1.0
         if (c.GT.100.0) then
            write(*,*) 'minimum sa nenaslo'
            go to 31
         end if
         d=abs(c-a)
         b=a+r*d
         go to 11
      end if
      call color('green')
      CALL HSYMBL(20)
      d=abs(c-a)
      b=a+r*d    
      fa=funct(a)
      fb=funct(b)
      fc=funct(c)
      i=0
      j=1
      jx=2400
      jy=1100
  21  continue
      if (abs(b-a).GT.abs(c-b)) then
       d=b-a
       x=a+r*d
       fx=funct(x)
       if (fb.GT.fx) then
         c=b
         b=x
         fc=fb
         fb=fx
       else
         a=x
         fa=fx
       end if
      else
       d=c-b
       x=b+r*d
       fx=funct(x)
       if (fb.GT.fx) then
         a=b
         b=x
         fa=fb
         fb=fx
       else
         c=x
         fc=fx
       end if
      end if   
      if (j.EQ.3) then
         j=1
         jy=jy+50
         ai=real(i)   
         call number(ai,-1,jx-100,jy)
         call number(x,5,jx,jy)
         i=i+1
         NX=NXPOSN(a)
         NY=NYPOSN(fa)
         CALL SYMBOL(21,NX,NY)
         NX=NXPOSN(b)
         NY=NYPOSN(fb)
         CALL SYMBOL(21,NX,NY)
         NX=NXPOSN(c)
         NY=NYPOSN(fc)
         CALL SYMBOL(21,NX,NY)
      end if
      if (abs(c-a).LE.0.001) then
         write(*,*) 'minimum je ',b,'  2.2135'
         go to 26
      end if    
      j=j+1
      go to 21
  26  NX=NXPOSN(0.5)
      NY=NYPOSN(-3.5)
      CALL MESSAG('minimum = ',NX,NY)
      NX=NXPOSN(1.7)
      call number(b,4,nx,ny)
      nx=nxposn(2.7)
      CALL MESSAG('/ presne = 2.2135',NX,NY) 
c      
  31  CALL DISFIN
      END 
c
      function funct(x)
c        a=0.1*(2.*rand()+1.)
         funct=2.0*exp(-0.53*x)*(x-2.2135)*(x-2.2135)-3.0
c         funct=2.0*(x-2.2135)**2-3.0
c        funct=0.01*x*x*x+a
      return
      end 
         
      