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

double func(double x1, double x2)
	{
	 return (2*x1*x1*x1+4*x1*x2*x2*x2-10*x1*x2+x2*x2);
	}
double df(double x1, double x2, double p1, double p2)
	{
	 return ((6*x1*x1+4*x2*x2*x2-10*x2)*p1+(12*x1*x2*x2-10*x1+2*x2)*p2);
	}

void Dihotomia()
{
 clrscr();
 double x10,x20,x1,x2,p1,p2,a1,a2,b1,b2,l1,l2,m1,m2,alfa,eps=0.01,xmin1,xmin2;
 int k=0;

 double delta=0.1*eps;

   x10=5;
   x20=2;
   p1=0;
   p2=1;

 double n=fabs((func(x10,x20)-df(x10,x20,p1,p2))/df(x10,x20,p1,p2));
 if(n>1) {alfa=1;}
 else {alfa=n;}

 x1=x10+alfa*p1;
 x2=x20+alfa*p2;

 if (func(x1,x2)>func(x10,x20))
	{
	 p1=-p1;
	 p2=-p2;
	}

 x1=x10+alfa*p1;
 x2=x20+alfa*p2;

 while(func(x10,x20)>func(x1,x2))
	{
	 x10=x1;
	 x20=x2;
	 alfa=2*alfa;
	 x1=x10+alfa*p1;
	 x2=x20+alfa*p2;
	}
 a1=x1-3*alfa*p1/2;
 a2=x2-3*alfa*p2/2;

 while ((a1>x1)||(a2>x2))
	{
	 if (a1>x1)
		{
		 b1=a1;
		 a1=x1;
		 b2=x2;
		}
	 else
		{
		 b1=x1;
		 b2=x2;
		};

	 if (a2>x2)
		{
		 b2=a2;
		 a2=x2;
		 b1=x1;
		}
	 else
		{
		 b1=x1;
		 b2=x2;
		}
	}

 cout<<"Ќ з «м­л© Ё­вҐаў « = "<<a1<<"     "<<b1<<"\n";
 cout<<"                     "<<a2<<"     "<<b2<<"\n";

 while(sqrt((b1-a1)*(b1-a1)+(b2-a2)*(b2-a2))>eps)
	{
	 l1=(a1+b1)/2-delta/2;
	 l2=(a2+b2)/2-delta/2;
	 m1=(a1+b1)/2+delta/2;
	 m2=(a2+b2)/2+delta/2;

	 if (func(l1,l2)<func(m1,m2))
		{
		 b1=m1;
		 b2=m2;
		}
	 else
		{
		 a1=l1;
		 a2=l2;
		}
	 k++;
	}

 xmin1=(a1+b1)/2;
 xmin2=(a2+b2)/2;

 cout<<"ЊЁ­Ё¬г¬            = "<<xmin1<<"\n";
 cout<<"                     "<<xmin2<<"\n";
 cout<<"—Ёб«® ЁвҐа жЁ© = "<<k;

 getch();
}


Соседние файлы в папке all