Скачиваний:
3
Добавлен:
01.05.2014
Размер:
3.79 Кб
Скачать
#include<math.h>
#include<conio.h>
#include<iostream.h>
#include<stdio.h>

double a,b,n,alphamin;
double x,y,px,py,plen,minx,miny;
int u=0;


double f(double alpha)
{
  return (x+alpha*px)*(x+alpha*px)+3*(y+alpha*py)*(y+alpha*py)+2*(x+alpha*px)*(y+alpha*py);
}

double df(double alpha)
{
  return (2*(x+alpha*px)+2*(y+alpha*py))*px+(6*(x+alpha*px)+2*(y+alpha*py))*py;
}


void swann()
{
  double t=0,h=0.00001;
  if(f(t)<f(t+h))
  {
	h=-h;
  }

  double x1,x2;
  x1=0;
  x2=x1+h;

  while(f(x1)>f(x2))
  {
	h=2*h;
	x1=x2;
	x2=x1+h;
  }

  if((x1-1.5*h)<x1)
  {
	  a=x1-1.5*h;
	  b=x1;
	  cout<<endl<<"SWENN: ( "<<a<<"  "<<b<<" )"<<endl;
  }
  else
  {
	  b=x1-1.5*h;
	  a=x1;
	  cout<<endl<<"SWENN: ( "<<a<<"  "<<b<<" )"<<endl;
  }
}


double ZS_2(double a,double b)
{
  double e,x1,x2;
  int k=0;
  
  e=0.000001;
  x1=a+0.618*fabs(b-a);
  while(fabs(b-a)>=e)
  {
    x2=a+b-x1;
    if((x1<x2)&&(f(x1)<f(x2)))
      b=x2;
    if((x1<x2)&&(f(x1)>=f(x2)))
    {
      a=x1;
      x1=x2;
    }
    if((x1>x2)&&(f(x1)<f(x2)))
      a=x2;
    if((x1>x2)&&(f(x1)>=f(x2)))
    {
      b=x1;
      x1=x2;
    }
	else
	{
		  return (a+b)/2;

	}
    k++;
	cout<<endl<<"("<<a<<";"<<b<<")";
  }
  
  return (a+b)/2;
}



double davidon()
{
  
  double e=0.000001,dfun,alphaa,alphab,h,alphar,z,w;
  dfun=df(0);
  
  h=2*fabs(f(0))/fabs(dfun);
  
  if(h>1) h=1;
  
  if(dfun>0)
  {
    px=-px;
    py=-py;
  }
  while(dfun*df(h)<0)
  {
    h=h+2*h;
  }
  
  alphaa=h/3;
  alphab=h;
  while(1)
  { u++;
    z=df(alphaa)+df(alphab)+3*(f(alphaa)-f(alphab))/alphab;
    w=sqrt(z*z-df(alphaa)*df(alphab));
    alphar=(w-df(alphaa)+z)/(2*w-df(alphaa)+df(alphab));
    
	if(df(alphar)<=e) return alphar;
    
	if(df(alphar)>0) 
	{
		alphab=alphar;
    }
	else 
	{
		alphaa=alphar;
  
	}
	
  }
}


void OUT(int q)
{ if(q==1)
	{
		if(a>b)
		{
			n=a;
			a=b;
			b=n;
		}
 
	cout<<endl<<"Pezultat metoda swenna promegutok (a,b):  ("<<a<<";"<<b<<")"<<endl;
	
	}
  
  if(q==2)
   {
	  cout<<endl<<"-----------------------------------------------------"<<endl;	
	  cout<<endl<<"Pezultat metoda ZS-2 promegutok (a,b) : "<<endl;
	 	
   }
  if(q==3)
  {
	  cout<<endl<<"Pezultat metoda Davidona: alphar="<<alphamin;
	  cout<<endl<<"-----------------------------------------------------"<<endl;
	  cout<<endl<<"SnaR eto nahodim minimum za "<<u<<" iteracii(u)" <<endl<<" v to4ke s koordinatami: ("<<minx<<" ; "<<miny<<")"<<endl;
  }
  
  if((q!=1)&&(q!=2)&&(q!=3)) 
  {
		cout<<"ERROR!!!";
  }
}

void main()
{
  cout<<"Welcome to the programm!                                       Avtor:Malgin S.A."<<endl<<"________________________________________________________________________________"<<endl;
  char exit;
 while(exit!='n')
 {
  cout<<"Vvedite koordinati na4alnoi to4ki X0(X;Y):"<<endl<<"X=";
  cin>>x;
  cout<<endl<<"Y=";
  cin>>y;
  
  cout<<endl<<"Vvedite koordinati napravlRuwego vektora P(X;Y):"<<endl<<"X=";
  cin>>px;
  cout<<endl<<"Y=";
  cin>>py;
 
  cout<<endl<<"Dlina P=";
  plen=sqrt(px*px+py*py);
  cout<<plen;
  
  px=px/plen;
  py=py/plen;
  plen=1;
  
  swann();
  
  OUT(1);
  
  OUT(2);
  
  alphamin=ZS_2(a,b);
  
  x=x+alphamin*px;
  y=y+alphamin*py;
 cout<<endl<<"-----------------------------------------------------"<<endl;
  cout<<endl<<"Polu4ennaR to4ka metodom ZS-2:( "<<x<<" ; "<<y<<" )"<<endl;
cout<<endl<<"-----------------------------------------------------"<<endl;
  
  alphamin=davidon();
  
  minx=x+alphamin*px;
  miny=y+alphamin*py;
  
  OUT(3);
  cout<<endl<<"-----------------------------------------------------"<<endl;
  cout<<endl<<"EWO?";
  cin>>exit;
 }
}
Соседние файлы в папке Лабораторная работа №31