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

struct Vector
{
 double x1,x2;
 Vector()
 {
  x1=0;
  x2=0;
 }
 Vector(double ax1,double ax2)
 {
  x1=ax1;
  x2=ax2;
 }
 const Vector operator * (const double &vec) const
 {
  return Vector(x1*vec,x2*vec);
 }

 const void operator = (const Vector &vec)
 {
  x1=vec.x1;
  x2=vec.x2;
 }

 const double operator * (const Vector &vec) const
 {
  return (x1*vec.x1+x2*vec.x2);
 }

 const Vector operator + (const Vector &vec) const
 {
  return Vector(x1+vec.x1,x2+vec.x2);
 }

 const Vector operator - (const Vector &vec) const
 {
  return Vector(x1-vec.x1,x2-vec.x2);
 }
};

double function(Vector vec)
{
 return(3*(vec.x1-2)*(vec.x1-2)+4*(vec.x2-4)*(vec.x2-4));
}

Vector IP(Vector x1,Vector x3,int &k,int i)
{
 if (i==0) x3=x1;
 Vector x2,z[9],e1,e2;
 double e=0.000001,h=0.2,y[9]={0},y1;
 while(k==1)
 {
  y1=function(x1);
  e1=Vector(1,0);
  e2=Vector(0,1);
  z[0]=x1;     
  z[1]=x3+e1*h;
  z[2]=x3-e1*h;
  z[3]=x3+e2*h;
  z[4]=x3-e2*h;
  z[5]=z[1]+e2*h;
  z[6]=z[1]-e2*h;
  z[7]=z[2]+e2*h;
  z[8]=z[2]-e2*h;
  for (int i=0;i<9;i++)
  {
   y[i]=function(z[i]);
   if (y[i]<y1)
   {
	y1=y[i];
	x2=z[i];
   }
  }
  if (function(x2)>=function(x1))
  {
   h/=2;
   if (h<e)
   {
	x2=x1;
	k=3;
   }
  }
  else k=2;
 }
 return(x2);
}

void main()
{
 clrscr();
 int k=1;
 Vector x1,x2,x3,x4;
 x1=Vector(2,4);
 x2=IP(x1,x3,k,0);
 if (k==3)
 {
  cout<<x2.x1<<endl<<x2.x2;
  exit(1);
 }
 while(1)
 {
  x3=x2*2-x1;
  x1=x2;
  k=1;
  x4=IP(x1,x3,k,1);
  if (k==3)
  {
   cout<<x4.x1<<endl<<x4.x2;
   exit(1);
  }
  x2=x4;
 }
 getch();
}
Соседние файлы в папке all