Скачиваний:
13
Добавлен:
01.05.2014
Размер:
1.52 Кб
Скачать
#include <conio.h>
#include <iostream.h>
#include <math.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 ((vec.x1-2)*(vec.x1-2)*(vec.x1-2)*(vec.x1-2)+(vec.x1-2*vec.x2)*(vec.x1-2*vec.x2));
}

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

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

double proizv(Vector vec,Vector p)
{
 Vector grad;
 grad=Vector(f_dx1(vec),f_dx2(vec));
 return (grad*p);
}

void Swann4(Vector& x1,double& a,double& b,Vector& p)
{
 double pr;
 Vector x2;
 pr=proizv(x1,p);
 if(pr>0) p=Vector(0,0)-p;
 b=fabs(function(x1)/pr-1);
 if(b>1) b=1;
 x2=x1+p*b;
 while(proizv(x1,p)*proizv(x2,p)>0)
 {
  a=b;
  b*=2;
  x1=x2;
  x2=x1+p*b;
 }
}

void main()
{
 clrscr();
 Vector p,x1,x2;
 double a=0,b=0;
 x1=Vector(0,3);
 p=Vector(1,0);
 Swann4(x1,a,b,p);
 cout<<a<<endl<<b;
 getch();
}
Соседние файлы в папке all