Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
2
Добавлен:
01.05.2014
Размер:
5.31 Кб
Скачать
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <conio.h>
#include <math.h>
#include <stdio.h>
//---------------------------------------------------------------------------
class myVector
{
public:
    double x,y;                         //default Constructor
    myVector(double x0=0, double y0=0){
        x=x0;
        y=y0;
        }                               //basic operators
    myVector operator + (const myVector &mV) {   //sum of two Vectors
        return myVector(x+mV.x,y+mV.y);
        }
    myVector &operator -(){                      //unary minus
        this->x = - this->x;
        this->y = - this->y;
        return *this;
        }
    myVector operator -(const myVector &mV) {    //difference of two vectors
        return myVector(x-mV.x,y-mV.y);
        }
    myVector operator *(const double i)  {       //multiply by Num    postfix
        return myVector(x*i,y*i);
        }
    double operator *(const myVector &mV){       //Scalar Multiply
        return (this->x * mV.x + this->y * mV.y);
        }
};
//---------------------------------------------------------------------------
                                                 //our function  f
    double fun(const myVector &mV){
        return (-12*mV.y+4*mV.x*mV.x+4*mV.y*mV.y-4*mV.x*mV.y);
        }
                                                 //gradient in point grad(point)
    inline myVector gradient(const myVector &point){
        return myVector(8*point.x-4*point.y,-12+8*point.y-4*point.x);
        }
                                                 //gradient in point multiply by direction = f'(grad,line)
    double d_fun(const myVector &point,const myVector &line){
        return gradient(point)*line;
        }
//---------------------------------------------------------------------------
                                                 //algorith Swann-4
    bool Swann4(const myVector &start_point, myVector &line,
                  double &a,double &b, myVector &answer){
        double y1,y2,alfa=30000000;
        int k=0;
        myVector x1,x2;
        y1 = d_fun(start_point,line);
        if (y1>0) -line;
        x1= start_point;
        a=0;
        do {
            x2 = x1 + line*alfa;
            y2 = d_fun(x2,line);
            b = alfa;
            alfa = 2*alfa;
            k++;
            if (!y2) {answer=x2; printf("Count steps Swann4= %d\n",k); return true;}
            if (y1*y2<0) break; else {y1=y2; x1 = x2; a=b;}
            } while (true);
        printf("Count steps Swann4= %d\n",k);    
        return false;
        }
//---------------------------------------------------------------------------
                                                 //algorith Boltsano
    bool Boltsano( myVector &start_point, myVector &line, double &a,double &b, myVector &answer){
        double eps=0.000001, y,c;
        int k=1;
        myVector x;
        c=(a+b)/2;
        x = start_point + line*c;
        y = d_fun(x,line);
        while (fabs(y)>eps && y){
            if (y>0) b=c; else a=c;
            c=(a+b)/2;
            x = start_point+line*c;
            y = d_fun(x,line);
            k++;
            }
        answer = x;
        printf("Count steps Boltsano= %d\n",k);
        if (y) return false; else {b=c; return true;}
    }
//---------------------------------------------------------------------------
                                                  //algorith Davidona
    void Davidon(myVector &start_point, myVector &line, double &a,double &b, myVector &answer){
        double eps=0.0000001,y, alfa0,r,z,w;
        myVector x1,x2,x;
        int k=0;
        alfa0=a;
        b=b-alfa0;
        a=0;
        do {
            x1 = start_point + line*a;
            x2 = start_point + line*b;
            z = d_fun(x1,line)+ d_fun(x2,line)+3*(fun(x1)-fun(x2))/b;
            w = sqrt(z*z-d_fun(x1,line)*d_fun(x2,line));
            r = b* (w - d_fun(x1,line)+z)/(2*w-d_fun(x1,line)+d_fun(x2,line));
            x = x1 + line*r;
            y = d_fun(x,line);
            k++;
            if (fabs(y)<=eps) break;
                else if (y<0) {b=b-r;a=0;alfa0+=r;} else b=r;
            } while (true);
        if (y) x = x1 + d_fun(r+alfa0,line);
        answer = x;
        b = a+r;
        printf("Count steps Davidon= %d\n",k);
    }
//---------------------------------------------------------------------------
int main()
{
myVector st_p(-0.5,1),line(1,0),x;
double a,b,y;
printf("___WELCOME TO LAB 3: METHODS OF LINE SEARCHING___ \n\n");
if (!Swann4(st_p,line,a,b,x)) {
    if (!Boltsano(st_p,line,a,b,x)) {
        Davidon(st_p,line,a,b,x);
        printf("Davidon gave answer\n x*=(%.3f,%.3f) \n and f(x*)= %.3f \n and alfa = %.3f\n",x.x,x.y,fun(x),b);
        } else  printf("The answer was found Boltsano,\n x*=(%.3f,%.3f),\n and f(x*)= %.3f \n and alfa = %.3f",x.x,x.y,fun(x),b);
    } else printf("The answer was found Swann4,\n x*=(%.3f,%.3f),\n and f(x*)= %.3f \n and alfa = %.3f",x.x,x.y,fun(x),b);
printf("\n\n<<PRESS ANY KEY TO EXIT>>");
getch();
}
//---------------------------------------------------------------------------

Соседние файлы в папке Лабораторная работа №36