Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Лабораторная работа №3

.CPP
Скачиваний:
5
Добавлен:
01.05.2014
Размер:
3.9 Кб
Скачать
// lab3.cpp :Вар 6 Лаба 3
//М6 - метод Больцано - ДСК;
//Функция f(x)	(x0)^t 	(x*)^t	p^t
//(x1- 2 )^4 + (x1 - 2x2)^2	( 0 ; 3)	( 2.7 ; 1.51)	( 44 ;-24)

//#include "stdafx.h"
#include <math.h>
#include <iostream.h>
#include <conio.h>



//double X[2] = {0, 3},p[2] = {44, -24};
double X[2] = {8, 9},p[2] = {1, 0};
//
double e2 = 0.0000001, e = e2,r=100000;

double y(double);
double dy(double);
double Swenn4(double);
double Bolstano(double, double, int);
double DSK(double);   //Returns Minimum, found with Method DSK (Point to start serching)

double ky(double a)
{
 double x[2] = {X[0] + a*p[0], X[1] + a*p[1]};
 return pow((x[0] -2),4) + pow((x[0] - 2*x[1]),2);
}

double y(double a)
{
 double x[2] = {X[0] + a*p[0], X[1] + a*p[1]};
 return 4*pow((x[0] -5),2) + pow((x[1] - 6),2);
}
double ky1(double a)
{
 double x[2] = {X[0] + a*p[0], X[1] + a*p[1]};
 return (x[0]*x[0]+3*x[1]*x[1]+2*x[0]*x[1]);
}

double dy(double ax)
{
 return (y(ax+e) - y(ax))/e;
}

double Swenn4(double x)
{
 int i=0; //Количество итераций
 double a = 0.0001, A;
 static double B = 0, f = 0;//какой вход
 if (f)  //f=0 не первый вход
  {
   A = B;
   B = 0;
   f = 0;
   return(A);
  }
 if (dy(x) > 0)
  {
   //p[0] = -p[0];
   //p[1] = -p[1];
   a=-a;
  }
 do
  {
   a = 2*a;
   x += a;
   i++;
  }
 while(dy(0)*dy(x) > 0);
 A = x-a;
 B = x;
 f = 1;
 cout<<"iteracii: "<<i<<"\n";
 return A;
}

double Bolstano(double A, double B)
{
 double x, L;
 int i=0;
 x = (A + B)/2;
 while((fabs(dy(x)) > e) && (L > e))
  {
   if (dy(x) < 0)
    A = x;
   else
    B = x;
   L = B - A;
   x = (A + B)/2;
   i++;
 }
 cout<<"Bolcano iteracii: "<<i<<"\n\n\n"<<A<<"__"<<B<<"\n\n\n";
 return x;
}

double DSK(double X1)
{
 double X2, a, b, c, d, h, Min;
 int i=0;

 h = 0.01*X1;
 if (!h)
  h = 0.1;
 X2 = X1 + h;
 if (y(X1) < y(X2)) //направление убывания
   h = -h;
 do
  {
   X2 = X1 + h;
   while(y(X1) > y(X2)) //убывает
    {
     h = 2*h;
     X1 = X2;
     X2 = X1 + h;
    }
   if (y(X1) < y((X1+X2)/2))
    {
     b = X1;
     c = (X1+X2)/2;
     a = 2*b - c;
    }
   else
    {
     a = X1;
     b = (X1+X2)/2;
     c = 2*b - a;
    }
   d = b + (b-a)*(y(a)-y(c))/(y(a) - 2*y(b) + y(c))/2;
   h = h/2;
   if (y(b) < y(d))
    X1 = b;
   else
    X1 = d;
   i++;
  }
 while((fabs((d - b)/b) > e) || (fabs((y(d) - y(b))/y(b)) > e2));
 Min = (b+d)/2;
 cout<<"Dsk iteracii: "<<i<<"\n";
 return Min;
}
double david(double a,double b)
{
	int k=1;
	double e=0.00000001,q,t,temp,dfun,n,h,z,w,a0;
	a=Swenn4(0);
	b=Swenn4(0);
	cout<<"\n\n\n\n"<<a<<"___"<<b<<"\n\n\n\n";
	getch();
	do
	{       t=r;
		z=dy(a)+dy(b)+3*(y(a)-y(b))/(a-b);
		temp=z*z-dy(a)*dy(b);
		w=sqrt(temp);
		r=(w-dy(a)+z)/(2*w-dy(a)+dy(b));
		r=r*(b-a)+a;
		q=dy(r);
		cout<<q<<"\n";
		if(fabs(dy(r))<=e) return r;
		if(dy(r)>0) b=r;
		else a=r;
		if(fabs(t-r)<=e)return r;

	}
	while(k<10);
	return r;
}

void main ()
{

 double a, b, c;
 clrscr();
 a = Swenn4(0);
 b = Swenn4(0);
 cout << "Swenn-4:  interval[" << a << ", " << b << "]\n";
 double A[2] = {X[0] + a*p[0], X[1] + a*p[1]}, B[2] = {X[0] + b*p[0], X[1] + b*p[1]};
 cout << "[ {" << A[0] << "," << A[1] << "} : {" << B[0] << "," << B[1] << "} ]\n";
 c = Bolstano(a, b);
 double C[2] = {X[0] + c*p[0], X[1] + c*p[1]};
 C[0] = X[0] + c*p[0];
 C[1] = X[1] + c*p[1];
 cout << "Method Bolstano: Min = " << c << " - {" << C[0] << "," << C[1] << "}\n";
 getch();
 c = david(a, b);
 cout<<"\n\n"<<c<<"\n\n";
 //cout<<"\n\n\n\n\nviwel";
 C[0] = X[0] + r*p[0];
 C[1] = X[1] + r*p[1];
 cout << "Method Davidona: Min = " << r << " - {" << C[0] << "," << C[1] << "}\n";


 c = DSK(0);
 C[0] = X[0] + c*p[0];
 C[1] = X[1] + c*p[1];
 cout << "Method DSK:      Min = " << c << " - {" << C[0] << "," << C[1] << "}\n";
 getch();
}