#include "declare.h"
 void minim(float aa,float *ss,int *is)
{
 extern int n;
 extern float *x1,*y;
 float  fi_1,fi_2,ll,min,rvn;
 int  i, j,N=7000;
 float *a,*b,*y1,h=1.0;
 a = new float [n];
 b = new float [n];
 y1 = new float [n];
  for(i=0;i<n;i++)
  {
   y[i] = x1[i];
   y1[i] = a[i] = y[i];
  }
  for(i=0; i<n; i++)
  {
    y[i] -= h*ss[i];
    y1[i] += h*ss[i];
  }
 fi_1 = f(y);
 fi_2 = f(y1);
 if((fi_2<aa) && (fi_2<fi_1))
  {
    h= -h;
    for(i=0;i<n;i++) y[i] = y1[i];
  }
 if((fi_1<aa) || (fi_2<aa))
 {
   *is=1;
   while(is)
   {
    for(i=0;i<n;i++) x1[i]=y[i];
    for(i=0;i<n;i++) y[i] -= h*ss[i];
    fi_1 = f(x1);
    if(fi_1<f(y))
    {
     for(i=0;i<n;i++) b[i] = y[i];
     *is=0;
     break;
    }
   }
 }
  else
     if((aa<fi_1) && (aa<fi_2))
       for(i=0;i<n;i++)
       {
	a[i] = y[i];
	b[i] = y1[i];
       }
 ll=0.0;
 for(i=0;i<n;i++) ll+=(b[i]-a[i])*(b[i]-a[i]);
 ll=sqrt(ll);
 min = f(x1);
 if(min>f(a))
 {
  min=f(a);
  for(i=0;i<n;i++) x1[i]=a[i];
 }
 for(j=0;j<N;j++)
 {
  rvn=rand()/(float)RAND_MAX*ll;
  for(i=0;i<n;i++) y[i] = a[i] +rvn*(b[i]-a[i]);
  if(min>f(y))
  {
    min=f(y);
    for(i=0;i<n;i++) x1[i] = y[i];
  }

 }
  delete y1;
  delete a;
  delete b;
  return;

}

