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

double min(double,double,double);
double f(double x)
{
	return 2*x*x+3*exp(-x);
}

void main()
{
	clrscr();
	cout<<"MIN = "<<min(0.0001,0,0.1);
	getch();
}
double min(double e, double x0,double h)
{
	double x1=x0,x2;
	while(f(x1) >= f(x1+h)) x1+=h;//{x1+=h;cout<<"\nf1="<<f(x1)<<" x1="<<x1-h;getch();}
	if(f(x0) > f(x0-h)) while(f(x1) >= f(x1-h)) x1-=h;
	double a=x1-2*h,b=x1;
	x1=a+(b-a)*0.382;
	x2=a+(b-a)*0.618;
	int k=1;
	while((b-a)>e && k<=10000)
	{
		if(f(x1)<f(x2))
		{
			b=x2;
			x2=x1;
			x1=a+0.382*(b-a);
		}
		else
		{
			a=x1;
			x1=x2;
			x2=a+0.618*(b-a);
		}
		k++;
	}
	if(k==10000) return -1;
	return (b+a)/2;

}


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