Скачиваний:
13
Добавлен:
01.05.2014
Размер:
4 Кб
Скачать
#include<iostream.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>
class Point
{ public:
  double a,b;
  Point operator+(Point ob2);
  Point operator-(Point ob2);
  Point operator*(Point ob2);
  Point operator*(double m);
  Point operator=(Point ob2);
  Point(double x,double y);

 };
 Point::Point(double x, double y)
 {a=x;
  b=y;
 } 
 Point Point :: operator+(Point ob2)
   { Point temp(0,0);
   temp.a = a + ob2.a;
   temp.b = b + ob2.b;
   return temp;
   }
 Point Point :: operator-(Point ob2)
   { Point temp(0,0);
   temp.a = a - ob2.a;
   temp.b = b - ob2.b;
   return temp;
   }
  Point Point :: operator*(Point ob2)
   { Point temp(0,0);
   temp.a = a*ob2.a;
   temp.b = b*ob2.b;
   return temp;
   }
    Point Point :: operator=(Point ob2)
   { 
   a = ob2.a;
   b = ob2.b;
   return *this;
   }
   Point Point:: operator*(double m)
   {
    Point temp(0,0);
    temp.a =a*m;
    temp.b=b*m;
   return temp;
   }

   double fx(Point p,int func)
   {
     if (func==1)
     return(p.a*p.a*p.a+p.b*p.b-3*p.a-2*p.b+2);
     if (func==2)
     return((p.a*p.a+p.b-11)*(p.a*p.a+p.b-11)+(p.a+p.b*p.b-7)*(p.a+p.b*p.b-7));
     }


   void main()
   {

     clrscr();
     double Sred;
   double Bet;
     Point x1(0,0);
     int func=2;
     cout<<"Labarotornaia rabota 8(Metod Nelder-Mida)"<<endl;
     cout<<endl;
     cout<<"Viberete functiy: 1-26, 2-31"<<endl;
     cin>>func;
     cout<<"Vvedite na4alnyy to4ky: "<<endl;
     cout<<"x1=";
     cin>>x1.a;
     cout<<"x2=";
     cin>>x1.b;
     Point x2(0,0);
     //cin>>x2.a;
     //cin>>x2.b;
     Point x3(0,0);
     //cin>>x3.a;
     //cin>>x3.b;
     Point x4(0,0);
     Point e1(1,0);
     Point e2(0,1);
      Point x0(0,0);
     Point xl(0,0);
     Point x5(0,0);
      Point xr(0,0);
     x2=x1+e1*0.5;
     x3=x1+e2*0.5;
     /*cout<<x1.a;
     cout<<" "<<x1.b<<endl;
     cout<<x2.a;
     cout<<" "<<x2.b<<endl;
     cout<<x3.a;
     cout<<" "<<x3.b<<endl;  */
     int i=0;      
     
     Bet=1;
     while (Bet>0.01&&i<1000)
     //while (i<35)
     {


     if (fx(x1,func)>fx(x2,func))
     {x4=x1;
      x1=x2;
      x2=x4;
     }
      if (fx(x2,func)>fx(x3,func))
     {x4=x2;
      x2=x3;
      x3=x4;
     }

      if (fx(x1,func)>fx(x3,func))
     {x4=x1;
      x1=x3;
      x3=x4;
     }
      if (fx(x1,func)>fx(x2,func))
     {x4=x1;
      x1=x2;
      x2=x4;
     }

    /* cout<<"x1:"<<x1.a;
     cout<<" "<<x1.b<<endl;
     cout<<"x2:"<<x2.a;
     cout<<" "<<x2.b<<endl;
     cout<<"x3:"<<x3.a;
     cout<<" "<<x3.b<<endl;


     cout<<fx(x1,func)<<" < "<<fx(x2,func)<<" < "<<fx(x3,func)<<endl;


     */

     x0=x1*0.5+x2*0.5;
    /* cout<<" x01="<<x0.a;
     cout<<" x02="<<x0.b;
      */
     xr=x0*2-x3;
     
   /*  cout<<"  xr1="<<xr.a;
     cout<<"  xr2="<<xr.b;
     cout<<"  f(xr)="<<fx(xr,func)<<endl;   */

     if (fx(xr,func)<fx(x1,func))
     {xl=x0+x0*2-x3*2;

      
      if (fx(xl,func)<fx(xr,func))
	 {x5=xl;}
	 x5=xr;
      }
      if (fx(xr,func)>=fx(x1,func)&&fx(xr,func)<=fx(x2,func))
      {x5=xr;}
      if (fx(xr,func)>fx(x2,func)&&fx(xr,func)<fx(x3,func))
      {x5=x0+xr*0.5-x3*0.5;}
      if(fx(xr,func)>=fx(x3,func))
      {x5=x0+x3*0.5-x0*0.5;
       /*if(fx(x5,func)>fx(x3,func))
       {
       x2=x1+x2*0.5-x1*0.5;
       x5=x1+x5*0.5-x1*0.5;
       }  */
      }
    x3=x5;
   i++;

    double fx1,fx2,fx3;
    fx1=fx(x1,func);
    fx2=fx(x2,func);
    fx3=fx(x3,func);
    Sred=0.333*(fx1+fx2+fx3);

   /*  cout<<" Sred="<<Sred<<endl;*/
   Bet=sqrt(0.333*(pow(fx1-Sred,2)+pow(fx2-Sred,2)+pow(fx3-Sred,2)));
  // cout<<" Bet="<<Bet; */

   //cout<<fx(x1,func)<<endl;
   }
   cout<<endl;
   cout<<endl;
   cout<<"Resultat"<<endl;
   cout<<"x1="<<x1.a<<"   x2="<<x1.b<<endl;
   //cout<<"Bet="<<Bet<<endl;
   cout<<endl;
   cout<<"Koli4estvo iteratsij: "<<i;

     getch();
   }
Соседние файлы в папке all